Directory

← Back

PrintService for Vaadin

Enables standard Java Print API in Vaadin clients

Author

Rating

Popularity

<100

PrintService for Vaadin makes it possible to enable printing in a Vaadin application much like you would in a Swing application. It supports the standard PrintService interface already available in Java and handles the transportation to the client behind the scenes. The addon prints silently without displaying a print dialog on each print.

The client is required to have Java installed as it uses a hidden applet for printing.

This Addon is experimental at the moment, but it should work for most cases. More documentation will be available later on.

Sample code

public class MyVaadinApplication extends Application {

    private Window window;
    private Table table = new Table();
    private RemotePrintServiceManager printServiceManager;

    private class HighResImagePrinter implements Printable {

        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            try {
                if (pageIndex > 0) {
                    return Printable.NO_SUCH_PAGE;
                }

                Graphics2D g2d = (Graphics2D) graphics;
                BufferedImage image = ImageIO.read(getClass().getResourceAsStream("/highres_image.jpg"));
                //Set us to the upper left corner
                g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
                AffineTransform at = new AffineTransform();
                at.translate(0, 0);

                //We need to scale the image properly so that it fits on one page.
                double xScale = pageFormat.getImageableWidth() / image.getWidth();
                double yScale = pageFormat.getImageableHeight() / image.getHeight();
                // Maintain the aspect ratio by taking the min of those 2 factors and using it to scale both dimensions.
                double aspectScale = Math.min(xScale, yScale);
                at.setToScale(aspectScale, aspectScale);
                g2d.drawRenderedImage(image, at);
                return Printable.PAGE_EXISTS;

            } catch (IOException ex) {
                throw new PrinterException(ex.getMessage());
            }
        }
    }

    @Override
    public void init() {

        window = new Window("My Vaadin Application");
        setMainWindow(window);
        Button button = new Button("Click Me");
        button.addListener(new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
                try {
                    PrintService printService = printServiceManager.getDefaultPrintService();
                    PrinterJob printerJob = PrinterJob.getPrinterJob();
                    printerJob.setPrintable(new HighResImagePrinter());
                    printerJob.setPrintService(printService);
                    printerJob.print();
                } catch (PrinterException ex) {
                    Logger.getLogger(MyVaadinApplication.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        });
        window.addComponent(table);
        window.addComponent(button);

        printServiceManager = RemotePrintServiceManager.getInstance(this);
        printServiceManager.addListener(new PrintServiceListChangedListener() {

            public void onPrintServiceListChanged(PrintServiceListChangedEvent event) {
                BeanContainer c = new BeanContainer(RemotePrintService.class);
                c.setBeanIdProperty("id");
                for (PrintService ps : event.getPrintServices()) {
                    c.addBean(ps);
                }
                table.setContainerDataSource(c);
            }
        });


    }
}
//Do this AFTER applet has loaded (use eventlistening on manager)
RemotePrintServiceManager manager = RemotePrintServiceManager.getInstance(getApplication());
PrintService ps = manager.getDefaultPrintService();

String name = ps.getName();

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

Again fixes for build(Should fix its availability in Vaadin directory)

Released
2011-07-18
Maturity
EXPERIMENTAL
License
GNU Affero General Public License v3.0

Compatibility

Framework
Vaadin 6.5+
Browser
Browser Independent

PrintService for Vaadin - Vaadin Add-on Directory

Enables standard Java Print API in Vaadin clients PrintService for Vaadin - Vaadin Add-on Directory
PrintService for Vaadin makes it possible to enable printing in a Vaadin application much like you would in a Swing application. It supports the standard PrintService interface already available in Java and handles the transportation to the client behind the scenes. The addon prints silently without displaying a print dialog on each print. The client is required to have Java installed as it uses a hidden applet for printing. This Addon is experimental at the moment, but it should work for most cases. More documentation will be available later on.
Author Homepage
Issue Tracker
Source Code
Discussion Forum

PrintService for Vaadin version 0.0.1
null

PrintService for Vaadin version 0.0.2
Removed unused classes from applet resulting in a much smaller applet. Improved building process.

PrintService for Vaadin version 0.0.3
Improved buildprocess.

PrintService for Vaadin version 0.0.4
Again fixes for build(Should fix its availability in Vaadin directory)

Online