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)
版权声明:本文标题:java - Out of Memory Error When Converting Large Dimension Images to PDF with Apache PDFBox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736558531a1944612.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论