12.7. Printing

Vaadin does not currently have any special support for printing. Printing on the server-side is, in any case, largely independent from the web UI of an application. You just have to take care that the printing does not block server requests, possibly by running printing in another thread.

For client-side printing, most browsers support printing the web page. Vaadin does not explicitly support launching the printing in browser, but you can easily use the JavaScript print() method that opens the print window of the browser.

final Button print = new Button("Print This Page");
print.addListener(new ClickListener() {
    public void buttonClick(ClickEvent event) {
        print.getWindow().executeJavaScript("print();");
    }
});

This button would print the current page, including the button itself. Often, you want to be able to print a report or receipt and it should not have any visible UI components. In such a case, you could offer it as a PDF resource, or you could open a new window, as is done below, and automatically launch printing.

// A button to open the printer-friendly page.
Button print = new Button("Click to Print");

print.addListener(new Button.ClickListener() {
	public void buttonClick(ClickEvent event) {
        // Create a window that contains what you want to print
        Window window = new Window("Window to Print");

        // Have some content to print
        window.addComponent(new Label(
                "<h1>Here's some dynamic content</h1>\n" +
                "<p>This is to be printed to the printer.</p>",
                Label.CONTENT_XHTML));

        // Add the printing window as a new application-level
        // window
        getApplication().addWindow(window);

        // Open it as a popup window with no decorations
        getWindow().open(new ExternalResource(window.getURL()),
                "_blank", 500, 200,  // Width and height 
                Window.BORDER_NONE); // No decorations

        // Print automatically when the window opens.
        // This call will block until the print dialog exits!
        window.executeJavaScript("print();");

        // Close the window automatically after printing
        window.executeJavaScript("self.close();");
    }
});

How the browser opens the window, as an actual (popup) window or just a tab, depends on the browser. Notice that calling the print() method in the window will block the entire application until the print dialog exits. After printing, we automatically close the window with another JavaScript call, as there is no close() method in Window.

Printing as PDF would not require creating a Window object, but you would need to provide the content as a static or a dynamic resource for the open() method. Printing a PDF file would obviously require a PDF viewer cabability (such as Adobe Reader) in the browser.

Table of Contents

Preface
I. Vaadin Core Framework
1. Introduction
1.1. Overview
1.2. Example Application Walkthrough
1.3. Support for the Eclipse IDE
1.4. Goals and Philosophy
1.5. Background
2. Getting Started with Vaadin
2.1. Setting up the Development Environment
2.2. Installing Vaadin
2.3. Your First Project with Vaadin
2.4. Creating a Project with NetBeans
2.5. Creating a Project with Maven
3. Architecture
3.1. Overview
3.2. Technological Background
3.3. Applications as Java Servlet Sessions
3.4. Client-Side Engine
3.5. Events and Listeners
4. Writing a Web Application
4.1. Overview
4.2. Managing the Main Window
4.3. Sub-Windows
4.4. Handling Events with Listeners
4.5. Referencing Resources
4.6. Shutting Down an Application
4.7. Handling Errors
4.8. Setting Up the Application Environment
5. User Interface Components
5.1. Overview
5.2. Interfaces and Abstractions
5.3. Common Component Features
5.4. Label
5.5. Link
5.6. TextField
5.7. TextArea
5.8. PasswordField
5.9. RichTextArea
5.10. Date and Time Input with DateField
5.11. Button
5.12. CheckBox
5.13. Selecting Items
5.14. Table
5.15. Tree
5.16. MenuBar
5.17. Embedded
5.18. Upload
5.19. Form
5.20. ProgressIndicator
5.21. Slider
5.22. LoginForm
5.23. Component Composition with CustomComponent
6. Managing Layout
6.1. Overview
6.2. Window and Panel Root Layout
6.3. VerticalLayout and HorizontalLayout
6.4. GridLayout
6.5. FormLayout
6.6. Panel
6.7. HorizontalSplitPanel and VerticalSplitPanel
6.8. TabSheet
6.9. Accordion
6.10. AbsoluteLayout
6.11. CssLayout
6.12. Layout Formatting
6.13. Custom Layouts
7. Visual User Interface Design with Eclipse
7.1. Overview
7.2. Creating a New Composite
7.3. Using The Visual Designer
7.4. Structure of a Visually Editable Component
8. Themes
8.1. Overview
8.2. Introduction to Cascading Style Sheets
8.3. Creating and Using Themes
8.4. Creating a Theme in Eclipse
9. Binding Components to Data
9.1. Overview
9.2. Properties
9.3. Holding properties in Items
9.4. Collecting Items in Containers
10. Vaadin SQLContainer
10.1. Architecture
10.2. Getting Started with SQLContainer
10.3. Filtering and Sorting
10.4. Editing
10.5. Caching, Paging and Refreshing
10.6. Referencing Another SQLContainer
10.7. Using FreeformQuery and FreeformStatementDelegate
10.8. Non-implemented methods of Vaadin container interfaces
10.9. Known Issues and Limitations
11. Developing New Components
11.1. Overview
11.2. Doing It the Simple Way in Eclipse
11.3. Google Web Toolkit Widgets
11.4. Integrating a GWT Widget
11.5. Defining a Widget Set
11.6. Server-Side Components
11.7. Using a Custom Component
11.8. GWT Widget Development
12. Advanced Web Application Topics
12.1. Special Characteristics of AJAX Applications
12.2. Application-Level Windows
12.3. Embedding Applications in Web Pages
12.4. Debug and Production Mode
12.5. Resources
12.6. Shortcut Keys
12.7. Printing
12.8. Google App Engine Integration
12.9. Common Security Issues
12.10. URI Fragment and History Management with UriFragmentUtility
12.11. Capturing HTTP Requests
12.12. Drag and Drop
12.13. Logging
12.14. Accessing Session-Global Data
13. Portal Integration
13.1. Deploying to a Portal
13.2. Creating a Portal Application Project in Eclipse
13.3. Portlet Deployment Descriptors
13.4. Portlet Hello World
13.5. Installing Vaadin in Liferay
13.6. Handling Portlet Requests
13.7. Handling Portlet Mode Changes
13.8. Non-Vaadin Portlet Modes
13.9. Vaadin Control Panel for Liferay
13.10. Vaadin IPC for Liferay
13.11. Remote Portlets with WSRP
14. Rapid Development Using Vaadin and Roo
14.1. Overview
14.2. Setting Up the Environment
14.3. Creating the Domain Model
14.4. Creating Vaadin Application and CRUD Views
14.5. Using Vaadin Add-ons in a Roo project
14.6. Customizing Views
14.7. Authentication and Authorization
14.8. Internationalization
14.9. Testing the Application
14.10. Exception Handling
14.11. Deploying to Cloud Foundry
II. Vaadin Add-ons
15. Using Vaadin Add-ons
15.1. Overview
15.2. Downloading Add-ons from Vaadin Directory
15.3. Compiling Add-on Widget Sets
15.4. Removing Add-ons
15.5. Using Add-ons in a Maven Project
16. Vaadin Calendar
16.1. Overview
16.2. Installing Calendar
16.3. Basic Use
16.4. Implementing an Event Provider
16.5. Configuring the Appearance
16.6. Drag and Drop
16.7. Using the Context Menu
16.8. Localization and Formatting
16.9. Customizing the Calendar
17. Vaadin Timeline
17.1. Overview
17.2. Using Timeline
17.3. Code example
18. Vaadin JPAContainer
18.1. Overview
18.2. Installing
18.3. Defining a Domain Model
18.4. Basic Use of JPAContainer
18.5. Entity Providers
18.6. Filtering JPAContainer
18.7. Querying with the Criteria API
18.8. Automatic Form Generation
18.9. Using JPAContainer with Hibernate
19. Mobile Applications with TouchKit
19.1. Overview
19.2. Considerations Regarding Mobile Browsing
19.3. Creating a Project Targeting Multiple Devices
19.4. Mobile User Interface Components
19.5. Mobile Features
19.6. Testing and Debugging on Mobile Devices
20. Vaadin TestBench
20.1. Overview
20.2. Installing Vaadin TestBench
20.3. Preparing an Application for Testing
20.4. Using Vaadin TestBench Recorder
20.5. Developing JUnit Tests
20.6. Taking and Comparing Screenshots
20.7. Running Tests in an Distributed Environment
20.8. Known Issues
A. User Interface Definition Language (UIDL)
A.1. API for Painting Components
A.2. JSON Rendering
B. Songs of Vaadin
Index