'Itext 7.1.6 Out of memory error while merging documents
When a collate is created from multiple documents, java.lang.OutOfMemoryError: Java heap space error is coming in the server and the application goes down.
Below is a sample code snippet,
PdfReader objReader = new PdfReader(new ByteArrayInputStream(content));
PdfDocument srcPdfDocument = new PdfDocument(objReader);
Document srcDocument = new Document(srcPdfDocument);
WriterProperties wp = new WriterProperties();
wp.setPdfVersion(PdfVersion.PDF_1_7);
PdfDocument destPdfDoc = new PdfDocument(new PdfWriter(baos,wp));
Document destDocument = new Document(destPdfDoc);
PdfMerger merger = new PdfMerger(destPdfDoc, true,true);
merger.merge(srcPdfDocument, 1, srcPdfDocument.getNumberOfPages());
//finally block
finally{
if(srcPdfDocument != null && !srcPdfDocument.isClosed()) {
srcPdfDocument.close();
}
if(srcDocument != null) {
srcDocument.close();
}
if(destPdfDoc != null && !destPdfDoc.isClosed()) {
destPdfDoc.close();
}
if(destDocument != null) {
destDocument.close();
}
}
If its large documents (file size in GBs) or documents with corrupted tag structure are collated (Error in server log -com.itextpdf.kernel.pdf.tagging.ParentTreeHandler Corrupted tag structure: encountered invalid marked content reference - it doesn't refer to any page or any mcid. This content reference will be ignored), the out of memory error is thrown.
Is there any way we can collate documents without keeping the bytes in the memory
using Itext 7. Please note, using Itext 5 (PdfCopy) the functionality works fine without issues.
Solution 1:[1]
a bit late reply. I had the same problem itext 7.2.1. I solved it by adding.
flushCopiedObjects()
Merging a 6000 page, 400 MiB file, works fine.
PdfDocument pdf = new PdfDocument(new PdfWriter(outfile));
PdfMerger merger = new PdfMerger(pdf);
for ( String file : listFilesToAdd ) {
PdfDocument fileAdd = new PdfDocument(new PdfReader(file));
merger.merge(fileAdd , 1, fileAdd.getNumberOfPages());
pdf.flushCopiedObjects(fileAdd);
fileAdd.close();
}
pdf.close();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Krys |
