javapdfboxfreeze

PDFBox 2.0.1 hangs rendering pdf page


I have an issue with PDFBox 2.0.1 as its not being able to render a PDF. I wouldn’t really care if PDFBox fails on a couple of files, but the thing is that the entire thread hangs and never returns for several minutes and the memory keeps building up and there doesn't seem to be an end in sight.

The problem seems to be with RenderImageWithDPI, this is how I call it:

PDFRenderer renderer = new PDFRenderer(document);
BufferedImage image = renderer.renderImageWithDPI(0, 96); //Gets stuck here
ImageIO.write(image, "PNG", new File(fileName));

The code gets stuck on that particular line and consumes CPU and memory. In netbeans I see this stack trace whenever I pause execution. Though I am not sure what is happening as I see PDFBox working but seems to have hit some sort of infinite loop.

enter image description here

The PDF in question can be downloaded from: https://drive.google.com/file/d/0B5zMlyl8rHwsY3Y1WjFVZlllajA/view?usp=sharing

Can someone help pls?


Solution

  • Are you on java 8 or java 9? As explained here, start java with this option:

    -Dsun.java2d.cmm=sun.java2d.cmm.kcms.KcmsServiceProvider 
    

    this is related to JDK8/9 having changed their color management system.

    The file is still slow to render (20-30 seconds), because it is very complex.

    (Btw rendering didn't hang. It just took very, very long, i.e. several minutes)

    Update 1: New since PDFBox 2.0.9: you mentioned you're creating thumbnails. You can now enable subsampling with PDFRender.setSubsamplingAllowed(true), this will reduce the memory used for images.

    Update 2: The file renders 4x faster since 2.0.25, probably due to PDFBOX-5310.