Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8580975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:56:25+00:00 2026-06-11T20:56:25+00:00

I am using Flying Saucer with IText in order to generate PDFs from an

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:56:27+00:00Added an answer on June 11, 2026 at 8:56 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently start using flying-saucer library to generate a pdf from html web page.
I am using Flying Saucer to generate pdf from webpages . I came across
I'm using xhtmlrenderer (Also known as Flying Saucer) with iText to convert HTML to
I'm creating a PDF using Flying Saucer (which dumps out CSS/HTML to iText to
I am using Flying Saucer to render some PDF documents from strings to HTML.
I'm using the flying-saucer library to generate a PDF in a servlet that then
I'm using a custom truetype font in a pdf generated by flying saucer xhtmlrenderer.
I am using Flying Saucer (xhtmlrenderer) to create a PDF that contains a hyperlink.
I'm using flying saucer xhtmlrenderer for building pdf documents. Everything worked fine until now
I'm trying to convert strict XHTML to PDF using flying saucer and iText. I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.