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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. The button in the above example would print the current page, including the
  40. button itself. You can hide such elements in CSS, as well as otherwise style the
  41. page for printing. Style definitions for printing are defined inside a
  42. [literal]#++@media print {}++# block in CSS.
  43. [[advanced.printing.opening]]
  44. == Opening a Print Window
  45. You can open a browser window with a special UI for print content and
  46. automatically launch printing the content.
  47. [source, java]
  48. ----
  49. public static class PrintUI extends UI {
  50. @Override
  51. protected void init(VaadinRequest request) {
  52. // Have some content to print
  53. setContent(new Label(
  54. "<h1>Here's some dynamic content</h1>\n" +
  55. "<p>This is to be printed.</p>",
  56. ContentMode.HTML));
  57. // Print automatically when the window opens
  58. JavaScript.getCurrent().execute(
  59. "setTimeout(function() {" +
  60. " print(); self.close();}, 0);");
  61. }
  62. }
  63. ...
  64. // Create an opener extension
  65. BrowserWindowOpener opener =
  66. new BrowserWindowOpener(PrintUI.class);
  67. opener.setFeatures("height=200,width=400,resizable");
  68. // A button to open the printer-friendly page.
  69. Button print = new Button("Click to Print");
  70. opener.extend(print);
  71. ----
  72. How the browser opens the window, as an actual (popup) window or just a tab,
  73. depends on the browser. After printing, we automatically close the window with
  74. JavaScript [methodname]#close()# call.
  75. (((range="endofrange", startref="term.advanced.printing.print")))
  76. (((range="endofrange", startref="term.advanced.printing.JavaScript.print")))
  77. [[advanced.printing.pdf]]
  78. == Printing PDF
  79. ((("PDF")))
  80. To print content as PDF, you need to provide the downloadable content as a
  81. static or a dynamic resource, such as a [classname]#StreamResource#.
  82. You can let the user open the resource using a [classname]#Link# component, or
  83. some other component with a [classname]#BrowserWindowOpener# extension. When such
  84. a link or opener is clicked, the browser opens the PDF in the browser, in an
  85. external viewer (such as Adobe Reader), or lets the user save the document.
  86. It is crucial to notice that clicking a [classname]#Link# or a
  87. [classname]#BrowserWindowOpener# is a client-side operation. If you get the
  88. content of the dynamic PDF from the same UI state, you can not have the link or
  89. opener enabled, as then clicking it would not get the current UI content.
  90. Instead, you have to create the resource object before the link or opener are
  91. clicked. This usually requires a two-step operation, or having the print
  92. operation available in another view.
  93. [source, java]
  94. ----
  95. // A user interface for a (trivial) data model from which
  96. // the PDF is generated.
  97. final TextField name = new TextField("Name");
  98. name.setValue("Slartibartfast");
  99. // This has to be clicked first to create the stream resource
  100. final Button ok = new Button("OK");
  101. // This actually opens the stream resource
  102. final Button print = new Button("Open PDF");
  103. print.setEnabled(false);
  104. ok.addClickListener(new ClickListener() {
  105. @Override
  106. public void buttonClick(ClickEvent event) {
  107. // Create the PDF source and pass the data model to it
  108. StreamSource source =
  109. new MyPdfSource((String) name.getValue());
  110. // Create the stream resource and give it a file name
  111. String filename = "pdf_printing_example.pdf";
  112. StreamResource resource =
  113. new StreamResource(source, filename);
  114. // These settings are not usually necessary. MIME type
  115. // is detected automatically from the file name, but
  116. // setting it explicitly may be necessary if the file
  117. // suffix is not ".pdf".
  118. resource.setMIMEType("application/pdf");
  119. resource.getStream().setParameter(
  120. "Content-Disposition",
  121. "attachment; filename="+filename);
  122. // Extend the print button with an opener
  123. // for the PDF resource
  124. BrowserWindowOpener opener =
  125. new BrowserWindowOpener(resource);
  126. opener.extend(print);
  127. name.setEnabled(false);
  128. ok.setEnabled(false);
  129. print.setEnabled(true);
  130. }
  131. });
  132. layout.addComponent(name);
  133. layout.addComponent(ok);
  134. layout.addComponent(print);
  135. ----
  136. (((range="endofrange", startref="term.advanced.printing")))