3636import java .util .concurrent .atomic .AtomicLong ;
3737import java .util .stream .Collectors ;
3838import org .apache .commons .io .IOUtils ;
39+ import org .apache .pdfbox .io .RandomAccessRead ;
40+ import org .apache .pdfbox .io .RandomAccessReadBuffer ;
41+ import org .apache .pdfbox .Loader ;
3942import org .apache .pdfbox .multipdf .Overlay ;
4043import org .apache .pdfbox .multipdf .PDFMergerUtility ;
4144import org .apache .pdfbox .pdmodel .PDDocument ;
@@ -629,12 +632,12 @@ public static Language getDefaultLanguage(IContext context) throws CoreException
629632 public static boolean mergePDF (IContext context , List <FileDocument > documents , IMendixObject mergedDocument ) throws IOException {
630633 if (getMergeMultiplePdfs_MaxAtOnce () <= 0 || documents .size () <= getMergeMultiplePdfs_MaxAtOnce ()) {
631634
632- List <InputStream > sources = new ArrayList <>();
635+ List <RandomAccessRead > sources = new ArrayList <>();
633636 try (ByteArrayOutputStream out = new ByteArrayOutputStream ()) {
634637 PDFMergerUtility mergePdf = new PDFMergerUtility ();
635638
636639 for (FileDocument file : documents ) {
637- InputStream content = Core .getFileDocumentContent (context , file .getMendixObject ());
640+ RandomAccessRead content = new RandomAccessReadBuffer ( Core .getFileDocumentContent (context , file .getMendixObject () ));
638641 sources .add (content );
639642 }
640643 mergePdf .addSources (sources );
@@ -648,7 +651,7 @@ public static boolean mergePDF(IContext context, List<FileDocument> documents, I
648651 } catch (IOException e ) {
649652 throw new RuntimeException ("Failed to merge documents" + e .getMessage (), e );
650653 } finally { // We cannot use try-with-resources because streams would be prematurely closed
651- for (InputStream is : sources ) {
654+ for (RandomAccessRead is : sources ) {
652655 is .close ();
653656 }
654657 }
@@ -675,8 +678,10 @@ public static boolean mergePDF(IContext context, List<FileDocument> documents, I
675678 public static boolean overlayPdf (IContext context , IMendixObject generatedDocumentMendixObject , IMendixObject overlayMendixObject , boolean onTopOfContent ) throws IOException {
676679 Logging .trace (LOGNODE , "Retrieve generated document" );
677680 try (
678- PDDocument inputDoc = PDDocument .load (Core .getFileDocumentContent (context , generatedDocumentMendixObject ));
679- PDDocument overlayDoc = PDDocument .load (Core .getFileDocumentContent (context , overlayMendixObject ));
681+ RandomAccessRead generatedDocumentInput = new RandomAccessReadBuffer (Core .getFileDocumentContent (context , generatedDocumentMendixObject ));
682+ RandomAccessRead overlayInput = new RandomAccessReadBuffer (Core .getFileDocumentContent (context , overlayMendixObject ));
683+ PDDocument inputDoc = Loader .loadPDF (generatedDocumentInput );
684+ PDDocument overlayDoc = Loader .loadPDF (overlayInput );
680685 ByteArrayOutputStream baos = new ByteArrayOutputStream ()) {
681686 Logging .trace (LOGNODE , "Overlay PDF start, retrieve overlay PDF" );
682687
0 commit comments