Anjana
Anjana

Reputation: 109

Merging PDF using PDFBOX give scratch file size of 65536 error on AWS lambda only

We are using PDFBOX to merge two PDFs

  1. File 1 : 35 KB
  2. File 2 : 32 KB

While merging, I am getting the error Expected scratch file size of 65536 but found 61440

I am using MemoryUsuageSetting using tempfile for the merge, as in

pdfMerger.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());

Below is the code in my merge routine

List<InputStream> sources (inputsteam of my files to merge)
mergedPDFOutputStream = new ByteArrayOutputStream();
cosStream = new COSStream();
PDFMergerUtility pdfMerger = new PDFMergerUtility();
pdfMerger.addSources(sources);
pdfMerger.setDestinationStream(mergedPDFOutputStream);
pdfMerger.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());

While calling the above merge routine in AWS lambda, we are getting the same error (Expected scratch file size of 65536 but found 61440)

However, if I call our PDF merge routine in a Java main program in my local machine, the files are generated successfully.

Below is the stack trace of the error

java.io.IOException: Expected scratch file size of 65536 but found 61440r   at org.apache.pdfbox.io.ScratchFile.enlarge(ScratchFile.java:237)r  at org.apache.pdfbox.io.ScratchFile.getNewPage(ScratchFile.java:167)r   at org.apache.pdfbox.io.ScratchFileBuffer.addPage(ScratchFileBuffer.java:126)r  at org.apache.pdfbox.io.ScratchFileBuffer.<init>(ScratchFileBuffer.java:84)r    at org.apache.pdfbox.io.ScratchFile.createBuffer(ScratchFile.java:403)r at org.apache.pdfbox.cos.COSStream.createRawOutputStream(COSStream.java:271)r   at org.apache.pdfbox.multipdf.PDFCloneUtility.cloneForNewDocument(PDFCloneUtility.java:119)r    at org.apache.pdfbox.multipdf.PDFCloneUtility.cloneForNewDocument(PDFCloneUtility.java:101)r    at org.apache.pdfbox.multipdf.PDFCloneUtility.cloneForNewDocument(PDFCloneUtility.java:140)r    at org.apache.pdfbox.multipdf.PDFCloneUtility.cloneForNewDocument(PDFCloneUtility.java:101)r    at org.apache.pdfbox.multipdf.PDFCloneUtility.cloneForNewDocument(PDFCloneUtility.java:110)r    at org.apache.pdfbox.multipdf.PDFCloneUtility.cloneForNewDocument(PDFCloneUtility.java:101)r    at org.apache.pdfbox.multipdf.PDFCloneUtility.cloneMerge(PDFCloneUtility.java:194)r at org.apache.pdfbox.multipdf.PDFCloneUtility.cloneMerge(PDFCloneUtility.java:225)r at org.apache.pdfbox.multipdf.PDFCloneUtility.cloneMerge(PDFCloneUtility.java:185)r at org.apache.pdfbox.multipdf.PDFCloneUtility.cloneMerge(PDFCloneUtility.java:225)r at org.apache.pdfbox.multipdf.PDFCloneUtility.cloneMerge(PDFCloneUtility.java:174)r at org.apache.pdfbox.multipdf.PDFMergerUtility.appendDocument(PDFMergerUtility.java:558)r   at org.apache.pdfbox.multipdf.PDFMergerUtility.legacyMergeDocuments(PDFMergerUtility.java:391)r at org.apache.pdfbox.multipdf.PDFMergerUtility.mergeDocuments(PDFMergerUtility.java:277)r

We are using PDFBox version 2.0.12

Can someone please guide on resolving this issue?

Thanks, Anjana

Upvotes: 0

Views: 1492

Answers (1)

Porter47
Porter47

Reputation: 21

I had the same issue in AWS, just changed the memory usage for the merge from

pdfMerger.mergeDocuments(MemoryUsageSetting.setupMixed(MEMORY_USE));

to

pdfMerger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());

And it worked for AWS Lambda,

Hope this helps someone else.

Regards

Upvotes: 2

Related Questions