JSF / Seam: How to download and print a dynamically generated PDF file without user intervention?

I have a JSF / Seam web application in which there is a page with a form, which when submitted (clicking a button) leads to the dynamic creation of a PDF file (in Java, on the server side) based on the form input. Currently, I work in that the resulting PDF file is returned to the browser as a file download, so the user can choose to save it or open it in Acrobat Reader for later printing. A.

What I would like to do is that the PDF code is sent to the browser and printed (on the client side) without additional user intervention (well, except for the Windows printer settings dialog box, about which I can do nothing).

The solution is similar to having a hidden iframe on the page into which the PDF file is loaded, and then calling .contentWindow.print() on the iframe . However, I do not know how to get a PDF file in an iframe via HttpServletResponse (in Java), and even more so how to automatically call print () on an iframe after loading pdf.

+4
source share
2 answers

However, I do not know how to get a PDF file in an iframe via HttpServletResponse (in Java)

Let <iframe src> point to the servlet URL that receives the InputStream PDF file and writes it to the OutputStream response. You can optionally pass the JSF bean properties as optional GET parameters so that you can control PDF generation.

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

The doGet() method of the servlet in question might 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 than automatically calling print () in an iframe after loading a PDF file.

Connect the function to <iframe onload> . However, you need to consider the behavior of the browser. Additional tips can be found in this question: How to print an IFrame from javascript in Safari / Chrome

+5
source

This is not the answer to your question, but you can go through the chrome print dialog using the kiosk mode (skip the print dialog):

How to disable print preview in Google Chrome version 38?

I hope this works for you :)

quoted from answer:

The command line argument is to disable-print-preview works on the latest version of Chrome, I just checked it myself.

0
source

All Articles