You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

advanced-printing.asciidoc 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ---
  2. title: Printing
  3. order: 6
  4. layout: page
  5. ---
  6. [[advanced.printing]]
  7. = Printing
  8. ((("printing", id="term.advanced.printing", range="startofrange")))
  9. Vaadin does not have any special support for printing. There are two basic ways
  10. to print - in a printer controlled by the application server or by the user from
  11. the web browser. Printing in the application server is largely independent of
  12. the UI, you just have to take care that printing commands do not block server
  13. requests, possibly by running the print commands in another thread.
  14. ((("[methodname]#print()#", id="term.advanced.printing.print",
  15. range="startofrange")))
  16. ((("JavaScript", "[methodname]#print()#",
  17. id="term.advanced.printing.JavaScript.print",
  18. range="startofrange")))
  19. For client-side printing, most browsers support printing the web page. You can
  20. either print the current or a special print page that you open. The page can be
  21. styled for printing with special CSS rules, and you can hide unwanted elements.
  22. You can also print other than Vaadin UI content, such as HTML or PDF.
  23. [[advanced.printing.browserwindow]]
  24. == Printing the Browser Window
  25. Vaadin does not have special support for launching the printing in browser, but
  26. you can easily use the JavaScript [methodname]#print()# method that opens the
  27. print window of the browser.
  28. ((("JavaScript", "[methodname]#execute()#")))
  29. [source, java]
  30. ----
  31. Button print = new Button("Print This Page");
  32. print.addClickListener(new Button.ClickListener() {
  33. public void buttonClick(ClickEvent event) {
  34. // Print the current page
  35. JavaScript.getCurrent().execute("print();");
  36. }
  37. });
  38. ----
  39. See the http://demo.vaadin.com/book-examples-vaadin7/book#advanced.printing.this[on-line example, window="_blank"].
  40. The button in the above example would print the current page, including the
  41. button itself. You can hide such elements in CSS, as well as otherwise style the
  42. page for printing. Style definitions for printing are defined inside a
  43. [literal]#++@media print {}++# block in CSS.
  44. [[advanced.printing.opening]]
  45. == Opening a Print Window
  46. You can open a browser window with a special UI for print content and
  47. automatically launch printing the content.
  48. [source, java]
  49. ----
  50. public static class PrintUI extends UI {
  51. @Override
  52. protected void init(VaadinRequest request) {
  53. // Have some content to print
  54. setContent(new Label(
  55. "<h1>Here's some dynamic content</h1>\n" +
  56. "<p>This is to be printed.</p>",
  57. ContentMode.HTML));
  58. // Print automatically when the window opens
  59. JavaScript.getCurrent().execute(
  60. "setTimeout(function() {" +
  61. " print(); self.close();}, 0);");
  62. }
  63. }
  64. ...
  65. // Create an opener extension
  66. BrowserWindowOpener opener =
  67. new BrowserWindowOpener(PrintUI.class);
  68. opener.setFeatures("height=200,width=400,resizable");
  69. // A button to open the printer-friendly page.
  70. Button print = new Button("Click to Print");
  71. opener.extend(print);
  72. ----
  73. See the http://demo.vaadin.com/book-examples-vaadin7/book#advanced.printing.open[on-line example, window="_blank"].
  74. How the browser opens the window, as an actual (popup) window or just a tab,
  75. depends on the browser. After printing, we automatically close the window with
  76. JavaScript [methodname]#close()# call.
  77. (((range="endofrange", startref="term.advanced.printing.print")))
  78. (((range="endofrange", startref="term.advanced.printing.JavaScript.print")))
  79. [[advanced.printing.pdf]]
  80. == Printing PDF
  81. ((("PDF")))
  82. To print content as PDF, you need to provide the downloadable content as a
  83. static or a dynamic resource, such as a [classname]#StreamResource#.
  84. You can let the user open the resource using a [classname]#Link# component, or
  85. some other component with a [classname]#PopupWindowOpener# extension. When such
  86. a link or opener is clicked, the browser opens the PDF in the browser, in an
  87. external viewer (such as Adobe Reader), or lets the user save the document.
  88. It is crucial to notice that clicking a [classname]#Link# or a
  89. [classname]#PopupWindowOpener# is a client-side operation. If you get the
  90. content of the dynamic PDF from the same UI state, you can not have the link or
  91. opener enabled, as then clicking it would not get the current UI content.
  92. Instead, you have to create the resource object before the link or opener are
  93. clicked. This usually requires a two-step operation, or having the print
  94. operation available in another view.
  95. [source, java]
  96. ----
  97. // A user interface for a (trivial) data model from which
  98. // the PDF is generated.
  99. final TextField name = new TextField("Name");
  100. name.setValue("Slartibartfast");
  101. // This has to be clicked first to create the stream resource
  102. final Button ok = new Button("OK");
  103. // This actually opens the stream resource
  104. final Button print = new Button("Open PDF");
  105. print.setEnabled(false);
  106. ok.addClickListener(new ClickListener() {
  107. @Override
  108. public void buttonClick(ClickEvent event) {
  109. // Create the PDF source and pass the data model to it
  110. StreamSource source =
  111. new MyPdfSource((String) name.getValue());
  112. // Create the stream resource and give it a file name
  113. String filename = "pdf_printing_example.pdf";
  114. StreamResource resource =
  115. new StreamResource(source, filename);
  116. // These settings are not usually necessary. MIME type
  117. // is detected automatically from the file name, but
  118. // setting it explicitly may be necessary if the file
  119. // suffix is not ".pdf".
  120. resource.setMIMEType("application/pdf");
  121. resource.getStream().setParameter(
  122. "Content-Disposition",
  123. "attachment; filename="+filename);
  124. // Extend the print button with an opener
  125. // for the PDF resource
  126. BrowserWindowOpener opener =
  127. new BrowserWindowOpener(resource);
  128. opener.extend(print);
  129. name.setEnabled(false);
  130. ok.setEnabled(false);
  131. print.setEnabled(true);
  132. }
  133. });
  134. layout.addComponent(name);
  135. layout.addComponent(ok);
  136. layout.addComponent(print);
  137. ----
  138. See the http://demo.vaadin.com/book-examples-vaadin7/book#advanced.printing.pdfgeneration[on-line example, window="_blank"].
  139. (((range="endofrange", startref="term.advanced.printing")))