Liu KeFeng created PDFBOX-4035:
----------------------------------

             Summary: Render PDF to image may be cause OOM
                 Key: PDFBOX-4035
                 URL: https://issues.apache.org/jira/browse/PDFBOX-4035
             Project: PDFBox
          Issue Type: Improvement
          Components: Rendering
    Affects Versions: 2.0.7
         Environment: Mac os 10.13.2
Ubuntu 16.04.2 LTS
            Reporter: Liu KeFeng


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

{code:java}
  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);
    }
  }
{code}

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

{code:java}
DataBufferByte dataBuffer = (DataBufferByte)raster.getDataBuffer();
{code}

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.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to