0

Using my spring controller I want to directly open the printing view of generated pdf.By now I am generating a pdf using iTextPDF and put it to the OutputStream (HttpServletResponse.getOutputStream()).It is downloading the pdf. I can open it in browser and print using print button. What I want is getting the print UI from the controller or without print UI send to the printer. I have added some of my controller method,

 String mimeType = "application/pdf";
 System.out.println("MIME type: " + mimeType);
 response.setContentType(mimeType);
 String headerKey = "Content-Disposition";
 String headerValue = String.format("theCoder379.PDF");
 response.setHeader(headerKey, headerValue);
 OutputStream outStream = response.getOutputStream();
 createPdf(outStream, theObject);
 outStream.close();

In createPdf(outStream, theObject) method it is adding the generated iText pdf using 'theObject' to the 'outStream'. How can I achieve this.

theCoder379
  • 197
  • 1
  • 3
  • 16

1 Answers1

0

It is not possible the way you want (from the server side).

By now I am generating a pdf using iTextPDF and put it to the OutputStream

Good. That's effectively all you can do from the server side code.

I can open it in browser and print using print button The browser loads an external application (pdf plugin/viewer) and let the plugin display the file you've generated. You have no control over it whatsoever.

However you may create your own page with embedded plugin.

<object data="./pdfServlet" type="application/pdf" width="100%" height="100%">
 <p>Alternative text - include a link <a href="myfile.pdf">to the PDF!</a>
 </p>
</object>

and try to invoke the print event in the client-side javascript.

window.print();

However - I see some potential issues

  • it may not work on all browsers and all version you may want
  • some people (myself included) are sensitive if a web page tries to do thing the are not explicitly asked for (such as printing)
gusto2
  • 11,210
  • 2
  • 17
  • 36