title: FlyingSaucer ITextRenderer finish errors (unbalanced save/restore and blank PDFs)
body: <p>I am using Flying Saucer with IText in order to generate PDFs from an HTML file, specifically utilizing the <code>org.xhtmlrenderer.pdf.ITextRenderer</code> class. My code is simple. The generation code is encapsulated in a method like so:</p>
  
  <pre><code>/* 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();
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>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.</p>
  
  <p>Now, I just realized that I am actually calling "finish" twice per render! One in the <code>createPDF()</code>errors call (passing true as the second parameter), and one explicit call to <code>finishPDF()</code>.</p>
  
  <p>This has been running for quite some time now, and it's managed to generate PDFs successfully <em>most</em> of the time. I've been encountering 2 different types of errors sporadically:</p>
  
  <ol>
  <li><p>A runtime exception due to unbalanced save/restore state operators. Sample stacktrace as follows:</p>
  
  <pre><code>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]
  </code></pre></li>
  <li><p>The PDF generated has missing/deformed sections or at worst, a blank page.</p></li>
  </ol>
  
  <p>For Issue 2, I am fairly confident it's due to calling finishPDF() twice. However, for Issue 1, it occurs <em>before</em> the calls to finishPDF() are executed, so I actually don't know if that's what's causing the problem.</p>
  
  <p>Has anyone had experience dealing with these 2 issues while using Flying Saucer together with iText?</p>