4

I have a JSF/Seam web app which has a page with a form which, when submitted (button clicked), causes a PDF file to be dynamically created (in Java, server side) based on the form input. Currently what I have working is that the resulting PDF is returned to the browser as a file download, so the user can opt to save it or open it in Acrobat Reader for subsequent printing.

What I would like to happen is that the PDF is sent to the browser and printed (client side) without further user intervention (well, other than perhaps the Windows printer options dialog appearing, about which there's nothing I could do).

The solution seems to be based on having a hidden iframe on the page into which the PDF is loaded and then calling .contentWindow.print() on the iframe. However, I have no idea how to get the PDF into the iframe via the HttpServletResponse (in Java), much less how to automatically call print() on the iframe once the pdf has been loaded.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
mluisbrown
  • 14,448
  • 7
  • 58
  • 86
  • 1
    Off-topic (at least not related to your core question) but you can bypass the print dialog on at least Chrome > v38. See http://stackoverflow.com/questions/31643913/how-to-disable-print-preview-on-google-chrome-ver-38 (reference found by @EnriqueSanMartin) – Kukeltje Jul 06 '16 at 20:27

2 Answers2

5

However, I have no idea how to get the PDF into the iframe via the HttpServletResponse (in Java)

Let the <iframe src> point to a servlet URL which gets an InputStream of the PDF and writes it to the OutputStream of the response. You can if necessary pass JSF bean properties as additional GET parameters so that you can control the PDF generation.

<iframe src="pdfServlet?foo=#{bean.foo}&amp;bar=#{bean.bar}"></iframe>

The doGet() method of the servlet in question can look like this:

String foo = request.getParameter("foo");
String bar = request.getParameter("bar");

response.setContentType("application/pdf");

InputStream input = generatePdfSomehowBasedOn(foo, bar);
OutputStream output = response.getOutputStream();
// Now just write input to output.

much less how to automatically call print() on the iframe once the pdf has been loaded.

Hook a function on <iframe onload>. You however need to take browser specific behaviours into account. More hints can be found in this question: How do I print an IFrame from javascript in Safari/Chrome

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

This is not an answer to your question, but you can by pass the print dialog in chrome using "kiosk" mode (skipping printing dialog) see:

How to disable print preview on Google Chrome ver 38?

I hope that works for you :)

quoted from answer:

The command-line argument --disable-print-preview works on the newest version of Chrome, I've just tested it myself.

Community
  • 1
  • 1
Enrique San Martín
  • 2,202
  • 7
  • 30
  • 51
  • i have to print some forms (like 1000+) so it's nice to do some script to automatize this process, similary i think that the question asked have some similar issues (like sending the pdf to the browser back and trigger the print procces), and the answer (my) is too long to add it to the comments, and i posted because reading the question i belivied that it's NOT possible to print automatically a page/doc/pdf (so to clarify that is my answer) – Enrique San Martín Jul 06 '16 at 20:00
  • _"and the answer (my) is too long to add it to the comments,"_ You can, see my comment above – Kukeltje Jul 06 '16 at 20:28