admin管理员组

文章数量:1123380

I'm using Apache PDFBox to convert image files into PDF format in Java. Here's a snippet of my code:

private static byte[] convertImageToPdf(byte[] imageBytes, String fileType) {
    try (PDDocument document = new PDDocument()) {
        PDPage page = new PDPage(PDRectangle.A4);
        document.addPage(page);

        PDImageXObject image = PDImageXObject.createFromByteArray(document, imageBytes, fileType);
        drawImageOnPage(document, page, image);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        document.save(outputStream);
        return outputStream.toByteArray();
    } catch (Exception e) {
        log.error("Failed to convert image to PDF", e);
        throw new RuntimeException("Failed to convert image to PDF", e);
    }
}

It works fine for most images, but I'm encountering Out of Memory errors for images with very large dimensions, even if the file size is quite small (e.g., a 12000 x 2000 pixel image that's under 10 MB). I have confirmed that there's over 200 MB of free memory available, so I suspect it's related to how PDFBox handles large dimensions rather than overall memory availability?

Error:

Caused by: javax.imageio.IIOException: Caught exception during read: 
    at java.desktop/com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1851)
    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1466)
    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1363)
    at org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.createFromByteArray(PDImageXObject.java:384)
    at com.example.FileConverter.convertImageToPdf(FileConverter.java:106)
    ... 33 more

Caused by: java.lang.OutOfMemoryError: Java heap space
    at java.desktop/java.awt.image.DataBufferByte.<init>(DataBufferByte.java:93)
    at java.desktop/java.awt.image.ComponentSampleModel.createDataBuffer(ComponentSampleModel.java:433)
    at java.desktop/java.awt.image.Raster.createWritableRaster(Raster.java:1121)
    at java.desktop/javax.imageio.ImageTypeSpecifier.createBufferedImage(ImageTypeSpecifier.java:1066)
    at java.desktop/javax.imageio.ImageReader.getDestination(ImageReader.java:2877)
    at java.desktop/com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1485)
    at java.desktop/com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1844)
    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1466)
    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1363)
    at app.package.PdfImageProcessor.processImage(PdfImageProcessor.java:384)
    at app.package.PdfImageProcessor.convertImageToPdf(PdfImageProcessor.java:106)
    at app.package.PdfImageProcessor.convertFileToPdf(PdfImageProcessor.java:41)
    at app.package.DocumentHandler.validateAndUpload(DocumentHandler.java:60)
    at app.package.DocumentHandler_Subclass.validateAndUpload(Unknown Source)
    at app.package.DocumentHandler_Subclass.lambda$process$1(Unknown Source)
    at framework.package.InvocationContext.proceed(InvocationContext.java:73)
    at framework.package.InvocationContext.proceed(InvocationContext.java:62)
    at framework.package.TransactionManager.invokeInTransaction(TransactionManager.java:136)
    at framework.package.TransactionManager.invokeInTransaction(TransactionManager.java:107)
    at framework.package.TransactionalInterceptor.doIntercept(TransactionalInterceptor.java:38)
    at framework.package.TransactionManager.intercept(TransactionManager.java:61)
    at framework.package.TransactionalInterceptor.intercept(TransactionalInterceptor.java:32)
    at framework.package.TransactionBeanInterceptor.intercept(Unknown Source)
    at framework.package.InvocationHandler.invoke(InvocationHandler.java:42)
    at framework.package.InvocationContext.perform(InvocationContext.java:30)
    at framework.package.InvocationContexts.performAroundInvoke(InvocationContexts.java:27)
    at app.package.DocumentHandler_Subclass.upload(Unknown Source)
    at app.package.DocumentHandler_ClientProxy.upload(Unknown Source)
    at app.package.MessageListener.onMessage(MessageListener.java:31)
    at app.package.MessageListener_ClientProxy.onMessage(Unknown Source)
    at messaging.package.JmsMessageConsumer.callMessageListener(JmsMessageConsumer.java:1225)
    at messaging.package.AsyncMessageHandler.callMessageListener(AsyncMessageHandler.java:782)

本文标签: javaOut of Memory Error When Converting Large Dimension Images to PDF with Apache PDFBoxStack Overflow