Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-4921

java.lang.OutOfMemoryError: Java heap space when convertif large pdf to tiff

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 2.0.20
    • Fix Version/s: None
    • Component/s: Rendering
    • Labels:
      None
    • Environment:
      Java Version: 11
      Java Runtime Version: 11+28
      Java Home: OpenJDK11_x64
      Java Vendor: Oracle Corporation
      Java Vendor URL: http://java.oracle.com/

      Description

      Hello, 

      I am faced to this issue when i want to convert only the first page of a pdf image to a tiff image file.

      This is my java method : 

      private static ByteArrayOutputStream extractFirstPageAsTiff(File pdfsource) throws IOException {
       ByteArrayOutputStream out = new ByteArrayOutputStream();
       ImageOutputStream imageOut = new MemoryCacheImageOutputStream(out);
       // Load the PDF 
       try (PDDocument pdf = PDDocument.load(pdfsource)) {
         // Initialize PDF renderer
         PDFRenderer ren = new PDFRenderer(pdf);
         // Setup Image Writer
         ImageWriter writer = ImageIO.getImageWritersBySuffix("tiff").next();
         writer.setOutput(imageOut);
         // Setup Image Writer Parameters
         ImageWriteParam params = writer.getDefaultWriteParam();
         params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
         params.setCompressionType(COMPRESSION_TYPE_GROUP4FAX);
      
         // Writer pages to the image writer
         // capture the page image to file
         BufferedImage src = ren.renderImageWithDPI(0, RESOLUTION);
         int[] cmap = new int[] { 0xFF000000, 0xFFFFFFFF };
         BufferedImage src4BitColourDepth = ConvertUtil.convert4(src, cmap);
         // Prepare the Image
         Writer writer.prepareWriteSequence(null);
         writer.writeToSequence(new IIOImage(src4BitColourDepth, null, null), params);
         // End Writer Sequence
         writer.endWriteSequence();
         imageOut.close();
        }
        return out;
      }
      

       

      Produce this stack : 

      Exception in thread "main" java.lang.OutOfMemoryError: Java heap spaceException in thread "main" java.lang.OutOfMemoryError: Java heap space at java.desktop/java.awt.image.DataBufferInt.<init>(DataBufferInt.java:75) at java.desktop/java.awt.image.Raster.createPackedRaster(Raster.java:467) at java.desktop/java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032) at java.desktop/java.awt.image.BufferedImage.<init>(BufferedImage.java:324) at org.apache.pdfbox.rendering.PDFRenderer.renderImage(PDFRenderer.java:296) at org.apache.pdfbox.rendering.PDFRenderer.renderImage(PDFRenderer.java:243) at org.apache.pdfbox.rendering.PDFRenderer.renderImageWithDPI(PDFRenderer.java:215) at sandbox.tess4j.DetectOrientation.extractFirstPageAsTiff(DetectOrientation.java:173)
      
      

      In attachement the pdf document After Orientation.pdf

       

      Run with : 

      Java Version: 11
      Java Runtime Version: 11+28
      Java Home: OpenJDK11_x64
      Java Vendor: Oracle Corporation
      Java Vendor URL: http://java.oracle.com/

       

        Attachments

        1. After Orientation.pdf
          348 kB
          Florent Juillet

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              fjuillet Florent Juillet
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: