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

Render PDF to image may be cause OOM

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Won't Fix
    • Affects Version/s: 2.0.7
    • Fix Version/s: None
    • Component/s: Rendering
    • Labels:
      None
    • Environment:
      Mac os 10.13.2
      Ubuntu 16.04.2 LTS
    • Flags:
      Important

      Description

      My project need render every pdf page to image, and use scale.
      This is my code:

        public void pdfToImage(String uuid, String date, String pdfFileLocation, String saveParentFolder) {
          File pdfFile = new File(pdfFileLocation);
          try (PDDocument doc = PDDocument.load(pdfFile, MemoryUsageSetting.setupTempFileOnly())) {
            PDFRenderer renderer = new PDFRenderer(doc);
            int page = doc.getNumberOfPages();
            String imagePath = saveParentFolder + "/" + date + "/" + uuid + "/";
            File folder = new File(imagePath);
            if (!folder.exists() && !folder.mkdirs()) {
              throw new IllegalStateException("create directory " + imagePath + " error");
            }
            for (int i = 0; i < page; i++) {
              int subPage = i;
              Future<Void> future = callerRunExecutor.submit(() -> {
                BufferedImage image = renderer.renderImageWithDPI(subPage, 220);
                String imageLocation = imagePath + (subPage + 1) + appProperties.folderPath.previewImageSuffix;
                writeImage(image, imageLocation);
                return null;
              });
              try {
                future.get(5L, TimeUnit.MINUTES);
              } catch (InterruptedException | ExecutionException | TimeoutException e) {
                LOGGER.warn("generate " + pdfFileLocation + " page of  " + subPage + " preview image error.", e);
              }
            }
          } catch (IOException e) {
            throw new ApiException(GatewayError.INTERNAL_ERROR, e);
          }
        }
      

      This code maybe cause OOM. The PDFBox class is DCTFilter, source code:

      DataBufferByte dataBuffer = (DataBufferByte)raster.getDataBuffer();
      

      The pdf only have one page, the page only have one image, the image size: 13653 * 18000,
      When rendering the image, trigger the user code, the user to choose whether to render, how to render.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              疯雪ing Liu KeFeng
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: