Search code examples
performancepdfitextflying-saucer

Speeding up display of iText/flying-saucer generated PDF print dialog


I have created a PDF using flying-saucer that displays a print dialog when opened. Here's the code I wrote to do this:

String inputFile = "firstdoc.xhtml";
String url = new File(inputFile).toURI().toURL().toString();
String outputFile = "firstdoc.pdf";

OutputStream os = new FileOutputStream(outputFile);

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);

os.close();

PdfReader reader = new PdfReader(outputFile);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("firstdocprint.pdf"));
stamper.setPageAction(PdfWriter.PAGE_OPEN, new PdfAction(PdfAction.PRINTDIALOG), 1); 
stamper.close();

When I open the PDF on my PC, the print dialog displays after a couple of seconds.

However, when I embed the PDF in a hidden iframe...

<iframe src="firstdocprint.pdf" style="display:none"></iframe>

...the PDF print dialog never shows up when the page is initially rendered.

The hidden iframe approach is working on a very simple "TEST" PDF. I would expect that the "firstdocprint.pdf" print dialog would show up within a couple of seconds as well as the PDF is only 2KB (the simple "TEST" PDF is only 1KB).

Is there a way programmatically to speed up the rendering of the PDF print dialog?


Solution

  • It looks like a couple of things have sped it up for me...

    • Remove unused/dead printers
    • Instead of using "display:none", render the iframe with a very small width and height:
      <iframe src="firstdocprint.pdf" style="width:2px;height:2px;"></iframe>
      I think I may absolute position it somewhere off the page as well or cover it with another element