(Aspose.PDF version 24.6):
…
import com.aspose.pdf.Document;
import com.aspose.pdf.facades.PdfViewer;
…
Document pdfDocument = new Document(inputPdfFile);
PdfViewer viewer = new PdfViewer();
viewer.bindPdf(pdfDocument);
viewer.printDocument();
// OR
viewer.printDocumentWithSettings(pageSettings, printerSettings);
…
I encountered an OutOfMemoryError:
Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
How can I optimize the input PDF file to avoid this error during printing? I have attached the sample input file that causes the issue. The inputPdfFile is only one page with couple images.
I did try different optimization options on the input pdf file Document, but no luck.
Any help or suggestions would be greatly appreciated.
To address the java.lang.OutOfMemoryError: Java heap space issue you are encountering while printing a PDF document using Aspose.PDF, you can optimize the PDF file to reduce its memory footprint. Here are some strategies you can implement:
Optimize PDF Size: Use the OptimizationOptions class provided by Aspose.PDF to remove unnecessary objects and compress images within the PDF. This can significantly reduce the size of the PDF, which may help alleviate memory issues during printing.
Here’s a code snippet to optimize the PDF:
import com.aspose.pdf.Document;
import com.aspose.pdf.OptimizationOptions;
// Load the PDF document
Document pdfDocument = new Document("inputPdfFile.pdf");
// Create optimization options
OptimizationOptions optimizationOptions = new OptimizationOptions();
optimizationOptions.setCompressImages(true);
optimizationOptions.setImageQuality(75); // Set image quality to 75%
optimizationOptions.setRemoveUnusedObjects(true);
optimizationOptions.setRemoveUnusedStreams(true);
// Optimize the document
pdfDocument.optimizeResources(optimizationOptions);
pdfDocument.save("optimizedPdfFile.pdf");
Image Compression: If your PDF contains images, consider compressing them. You can set the image quality to a lower percentage to reduce the size of the images, which can help in managing memory usage.
Remove Unused Objects: Ensure that any duplicate or unused objects in the PDF are removed. This can be done using the optimization options mentioned above.
Adjust Java Heap Size: If the optimizations do not resolve the issue, consider increasing the Java heap size. You can do this by setting the -Xmx parameter when starting your Java application. For example, to set the maximum heap size to 512 MB, you would use:
java -Xmx512m -jar yourApplication.jar
Convert to Grayscale: If applicable, converting the PDF from RGB to grayscale can also reduce the file size and improve printing performance. This can be done using specific features in Aspose.PDF.
By applying these optimizations, you should be able to reduce the memory consumption during the printing process and avoid the OutOfMemoryError. If you continue to experience issues, please provide additional details about the PDF content or consider reaching out for further assistance.
Have you tried with 24.12 version of the API and increased the Java Heap Size? Can you please share which JDK version you are using and what is the size of RAM you currently have?
I did try increase memory several times up to 2GB and ran, but no luck either.
-Xms2g -Xmx2g
BTW, I was able to test printing 200 page-pdf with bunch of small images ok, no issue. Just happen when testing printing this 1 page pdf, that has couple images, cause memory issue.
The issue may be occurring due to an older version of the API. Therefore, we request you please try with 24.12 version at your end and if issue still persists, please let us know by sharing complete sample code snippet and stack trace.