javaitextflying-saucer

FlyingSaucer ITextRenderer finish errors (unbalanced save/restore and blank PDFs)


I am using Flying Saucer with IText in order to generate PDFs from an HTML file, specifically utilizing the org.xhtmlrenderer.pdf.ITextRenderer class. My code is simple. The generation code is encapsulated in a method like so:

/* PDfGenerator class only has ONE instance */
public PdfGenerator() {
    this.renderer = new ITextRenderer(); //This is a class variable that only gets instantiated ONCE
}

public void generatePDF(String outputFilePath, String htmlContent) {
    renderer.setDocumentFromString(htmlContent);
    renderer.layout();
    renderer.createPDF(new BufferedOutputStream(
          new FileOutputStream(new File(outputFilePath)), BUFFER_SIZE), true);
    renderer.finishPDF();
}

EDIT:

My generator class is actually managed by Spring as a singleton object. I have a manager class that has an ExecutorService acting as a queue for PDF generation tasks. This manager uses the singleton Generator to generate objects. Therefore I instantiate the ITextRenderer just ONCE and just reuse it. Now I set my queue to operate up to 2 threads concurrently. I just realized if maybe this is causing the, because I encounter situations where TWO threads are using my ONE renderer to render two separate sets of PDFs.

Now, I just realized that I am actually calling "finish" twice per render! One in the createPDF()errors call (passing true as the second parameter), and one explicit call to finishPDF().

This has been running for quite some time now, and it's managed to generate PDFs successfully most of the time. I've been encountering 2 different types of errors sporadically:

  1. A runtime exception due to unbalanced save/restore state operators. Sample stacktrace as follows:

    java.lang.RuntimeException: Unbalanced save/restore state operators.
    at com.lowagie.text.pdf.PdfContentByte.restoreState(Unknown Source) ~[itext-2.0.8.jar:na]
    at org.xhtmlrenderer.pdf.ITextOutputDevice.setClip(ITextOutputDevice.java:737) ~[core-renderer-R8.jar:na]
    at org.xhtmlrenderer.pdf.ITextRenderer.paintPage(ITextRenderer.java:387) ~[core-renderer-R8.jar:na]
    at org.xhtmlrenderer.pdf.ITextRenderer.writePDF(ITextRenderer.java:348) ~[core-renderer-R8.jar:na]
    at org.xhtmlrenderer.pdf.ITextRenderer.createPDF(ITextRenderer.java:315) ~[core-renderer-R8.jar:na]
    at org.xhtmlrenderer.pdf.ITextRenderer.createPDF(ITextRenderer.java:280) ~[core-renderer-R8.jar:na]
    
  2. The PDF generated has missing/deformed sections or at worst, a blank page.

For Issue 2, I am fairly confident it's due to calling finishPDF() twice. However, for Issue 1, it occurs before the calls to finishPDF() are executed, so I actually don't know if that's what's causing the problem.

Has anyone had experience dealing with these 2 issues while using Flying Saucer together with iText?


Solution

  • The issue I've encountered is basically accessing the same ITextRenderer instance concurrently on different threads, thus messing with the processing of the currently running PDF generation job.

    I've made the Executor service just use 1 thread and made the generation jobs sequential, and based on my observation, I can reuse the same ITextRenderer as long as I do the processing one at a time.

    I've sent a query to confirm this with the devs of the Flying Saucer lib, I'll just update this post when they reply.

    As an aside, I just have to figure out if it's more efficient to generate using 1 thread and 1 pre-instantiated ITextRenderer instance or use multiple threads but instantiate the ITextRenderer per-thread.