6
  • My pdf is 20 MB and four pages.
  • When my program crashes, I have 1 GB of free RAM
  • I have tried adding setupTempFileOnly to the load method without any change.
  • Used Memory first loop 217 MB, Used Memory second loop 220 MB
  • I get two jpg files but my program crash third loop, on page 3.
  • I want to export all pages as jpg with the highest jpg quality (low jpg compression)
  • I use the latest Java version and pdfbox 2.0.20

My pdf is not very large and has only four pages. Can I optimize my code to use less memory? Can I do someone clean after each loop?

My code

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.FileImageOutputStream;

import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;

import net.coobird.thumbnailator.Thumbnails;

public class PdfToImage {
       public static void main (String args[]) throws IOException {
           File file = new File("C:/myPath/printfile.pdf");
            PDDocument document = PDDocument.load(file, MemoryUsageSetting.setupTempFileOnly());
            PDFRenderer renderer = new PDFRenderer(document);
           
           try {
               int pageNumber = 0;
               for( PDPage page : document.getPages() )
                {
        
        float trimboxWidth = page.getTrimBox().getWidth();
        float trimboxHeight = page.getTrimBox().getHeight();
        
        float mediaboxWidth = page.getMediaBox().getWidth();
        float mediaboxHeight = page.getMediaBox().getHeight();
        
        float getY = (mediaboxHeight - trimboxHeight) / 2;
        float getX = (mediaboxWidth - trimboxWidth) / 2;
        
        PDRectangle rectangle = new PDRectangle(getX, getY, trimboxWidth, trimboxHeight);
        
        //Crop the page
        page.setCropBox(rectangle);
        
        //Rendering an image from the PDF document
        BufferedImage image = renderer.renderImageWithDPI(pageNumber, 96, ImageType.RGB);
                
        //Resize the image
        BufferedImage thumbnail = 
                Thumbnails.of(image)
                    .height(700)
                    .asBufferedImage();

        //Writing the image to a file
        JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
        jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            jpegParams.setCompressionQuality(1f);
        
        final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
        // specifies where the jpg image has to be written
        writer.setOutput(new FileImageOutputStream(
          new File("C:/myPath/myimage" + pageNumber + ".jpg")));
        
        writer.write(null, new IIOImage(thumbnail, null, null), jpegParams);

            System.out.println("Used Memory: " +  (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
       pageNumber++; 
            
                }
           }
           finally
            {
                if( document != null )
                {
                    document.close();
                }

            } 
       }
}

Error message third loop

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.HashMap.replacementTreeNode(Unknown Source) at java.util.HashMap.treeifyBin(Unknown Source) at java.util.HashMap.putVal(Unknown Source) at java.util.HashMap.put(Unknown Source) at org.apache.pdfbox.pdmodel.graphics.shading.TriangleBasedShadingContext.calcPixelTable(TriangleBasedShadingContext.java:121) at org.apache.pdfbox.pdmodel.graphics.shading.PatchMeshesShadingContext.calcPixelTable(PatchMeshesShadingContext.java:280) at org.apache.pdfbox.pdmodel.graphics.shading.TriangleBasedShadingContext.createPixelTable(TriangleBasedShadingContext.java:80) at org.apache.pdfbox.pdmodel.graphics.shading.PatchMeshesShadingContext.(PatchMeshesShadingContext.java:71) at org.apache.pdfbox.pdmodel.graphics.shading.Type7ShadingContext.(Type7ShadingContext.java:46) at org.apache.pdfbox.pdmodel.graphics.shading.Type7ShadingPaint.createContext(Type7ShadingPaint.java:63) at sun.java2d.pipe.AlphaPaintPipe.startSequence(Unknown Source) at sun.java2d.pipe.AAShapePipe.renderTiles(Unknown Source) at sun.java2d.pipe.AAShapePipe.renderPath(Unknown Source) at sun.java2d.pipe.AAShapePipe.fill(Unknown Source) at sun.java2d.pipe.PixelToParallelogramConverter.fill(Unknown Source) at sun.java2d.pipe.ValidatePipe.fill(Unknown Source) at sun.java2d.SunGraphics2D.fill(Unknown Source) at org.apache.pdfbox.rendering.PageDrawer.shadingFill(PageDrawer.java:1388) at org.apache.pdfbox.contentstream.operator.graphics.ShadingFill.process(ShadingFill.java:42) at org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:932) at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:510) at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:484) at org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:156) at org.apache.pdfbox.rendering.PageDrawer.drawPage(PageDrawer.java:271) at org.apache.pdfbox.rendering.PDFRenderer.renderImage(PDFRenderer.java:321) at org.apache.pdfbox.rendering.PDFRenderer.renderImage(PDFRenderer.java:243) at org.apache.pdfbox.rendering.PDFRenderer.renderImageWithDPI(PDFRenderer.java:229) at PdfToImage.main(PdfToImage.java:47)

Edit I found i I change

//Rendering an image from the PDF document
BufferedImage image = renderer.renderImageWithDPI(pageNumber, 96, ImageType.RGB);

to

//Rendering an image from the PDF document
BufferedImage image = renderer.renderImageWithDPI(pageNumber, 50, ImageType.RGB);

It works fine, no crash. But its not a solution, or a bad solution to lower resolution that much.

5
  • pdfbox.apache.org/2.0/… Commented Sep 7, 2020 at 9:09
  • @TilmanHausherr Can you see anything in your link I'am not doing in my code except increase the -Xmx value? I have setupTempFileOnly(), I'am not using a list, I'am closing the document, I have lowered renderImageWithDPI() but 50 dpi is a little bit to low (96 dpi is fine).
    – Xtreme
    Commented Sep 8, 2020 at 18:41
  • 2
    the resources thing. However you're using temp files so this may not help. Not mentioned in the list but worth a try is to enable subsampling in PDFRenderer by calling setSubsamplingAllowed(true). Commented Sep 9, 2020 at 6:51
  • 1
    @TilmanHausherr Just spent 5 hours trying to fix my clients Heap out of memory error when creating thumbnails for pdfs.... setSubsamplingAllowed(true) completely fixed the issue! The heap stays at a very modest level while processing multiple pdfs, before just a single pdf was pushing it past it's max
    – RobOhRob
    Commented Aug 31, 2021 at 21:09
  • @RobOhRob did you find a major difference to image quality when extracting for thumbnails (i.e. small sizes) using setSubsamplingAllowed(true)? I'm interested in this for the same reason. Thx.
    – AWeber
    Commented Aug 10, 2022 at 21:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.