2

I am trying to generate PDF from Html using iText and Flying Saucer. I have used different techniques suggested on SO to enable DocumentBuilder parse quickly.

However, ITextRenderer createPdf(outputstream) has become a bottleneck. It is extremely slow and I have no idea how to improve the speed of the process.

Any help would be really appreciated.

private Document getDocument(String htmlContent) throws Exception
{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    //For faster document.
    factory.setNamespaceAware(false);
    factory.setValidating(false);
    factory.setFeature("http://xml.org/sax/features/namespaces", false);
    factory.setFeature("http://xml.org/sax/features/validation", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setEntityResolver(FSEntityResolver.instance());
    return builder.parse(new ByteArrayInputStream(htmlContent.getBytes()));
}

public void printHtmlToPdf(final String htmlContent, String tempFile, String title) throws Exception
{
    Document document =  getDocument(htmlContent);
    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocument(document, null);
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(tempFile)); 
    renderer.layout();
    renderer.createPDF(outputStream);
    outputStream.close();
}
TJ-
  • 14,085
  • 12
  • 59
  • 90

4 Answers4

3

I had the same problem, PDF creation was very slow and the <IMG src="http:..."> were not resolved.

It was because the HTTP proxy was not used and I guess there was a lot of "http://" timeouts.

In my situation, the fix was

System.setProperty("java.net.useSystemProxies", "true");
RealHowTo
  • 34,977
  • 11
  • 70
  • 85
  • Thanks for the suggestion. However, I don't have images (or any other resource that has to be fetched externally from the web). – TJ- Sep 14 '12 at 05:43
0

It might just be a buffering issue. If you haven't done so already, trying wrapping the output stream with a BufferedOutputStream and using that as the createPdf argument.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

The following piece should solve it.

package com.pdf.web;

import com.lowagie.text.DocumentException;
import java.io.*;
import org.xhtmlrenderer.layout.SharedContext;
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xhtmlrenderer.resource.XMLResource;
import org.xml.sax.InputSource;

// Referenced classes of package com.pdf.web:
//            PDFRender

public class createPDF
{

    public createPDF()
    {
    }

    public createPDF(String url, String pdf)
        throws IOException, DocumentException
    {
        OutputStream os;
        os = null;
        os = new FileOutputStream(pdf);
        ITextRenderer renderer = new ITextRenderer();
        PDFRender.ResourceLoaderUserAgent callback = new PDFRender.ResourceLoaderUserAgent(renderer.getOutputDevice());
        callback.setSharedContext(renderer.getSharedContext());
        renderer.getSharedContext().setUserAgentCallback(callback);
        org.w3c.dom.Document doc = XMLResource.load(new InputSource(url)).getDocument();
        renderer.setDocument(doc, url);
        renderer.layout();
        renderer.createPDF(os);
        os.close();
        os = null;
        if(os != null)
        {
            try
            {
                os.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
        break MISSING_BLOCK_LABEL_143;
        Exception exception;
        exception;
        if(os != null)
        {
            try
            {
                os.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
        throw exception;
    }
}
rocky
  • 11
  • 1
  • 4
0

I had a similar problem with ITextRenderer and that was mitigated when I fixed the createReplacedElement method for finding additional resources like images and such.

The createReplacedElement method is part of the interface ReplacedElementFactory.

I used this page as base when I created my project and there you can read about createReplacedElement among other things: https://www.baeldung.com/java-html-to-pdf

javabeangrinder
  • 6,939
  • 6
  • 35
  • 38