From 1f10693eafdd60c34443a44c22641773496e77ee Mon Sep 17 00:00:00 2001 From: sbplat <71648843+sbplat@users.noreply.github.com> Date: Thu, 28 Dec 2023 03:23:55 +0000 Subject: [PATCH 1/4] fix: sequentially convert each pdf page into a BufferedImage to avoid getting MLE for large pdf files --- .../stirling/software/SPDF/utils/PdfUtils.java | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java index 22e09d6a..000a8294 100644 --- a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java @@ -205,11 +205,6 @@ public class PdfUtils { int pageCount = document.getNumberOfPages(); List images = new ArrayList<>(); - // Create images of all pages - for (int i = 0; i < pageCount; i++) { - images.add(pdfRenderer.renderImageWithDPI(i, DPI, colorType)); - } - // Create a ByteArrayOutputStream to save the image(s) to ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -226,8 +221,8 @@ public class PdfUtils { writer.setOutput(ios); writer.prepareWriteSequence(null); - for (int i = 0; i < images.size(); ++i) { - BufferedImage image = images.get(i); + for (int i = 0; i < pageCount; ++i) { + BufferedImage image = pdfRenderer.renderImageWithDPI(i, DPI, colorType); writer.writeToSequence(new IIOImage(image, null, null), param); } @@ -240,8 +235,9 @@ public class PdfUtils { BufferedImage combined = new BufferedImage(images.get(0).getWidth(), images.get(0).getHeight() * pageCount, BufferedImage.TYPE_INT_RGB); Graphics g = combined.getGraphics(); - for (int i = 0; i < images.size(); i++) { - g.drawImage(images.get(i), 0, i * images.get(0).getHeight(), null); + for (int i = 0; i < pageCount; ++i) { + BufferedImage image = pdfRenderer.renderImageWithDPI(i, DPI, colorType); + g.drawImage(image, 0, i * image.getHeight(), null); } // Write the image to the output stream @@ -253,8 +249,8 @@ public class PdfUtils { } else { // Zip the images and return as byte array try (ZipOutputStream zos = new ZipOutputStream(baos)) { - for (int i = 0; i < images.size(); i++) { - BufferedImage image = images.get(i); + for (int i = 0; i < pageCount; ++i) { + BufferedImage image = pdfRenderer.renderImageWithDPI(i, DPI, colorType); try (ByteArrayOutputStream baosImage = new ByteArrayOutputStream()) { ImageIO.write(image, imageType, baosImage); From 1d243a0ca5c62a7812531d981d92feaf819551f4 Mon Sep 17 00:00:00 2001 From: sbplat <71648843+sbplat@users.noreply.github.com> Date: Wed, 27 Dec 2023 22:43:30 -0500 Subject: [PATCH 2/4] fix: clean up redundant variable --- src/main/java/stirling/software/SPDF/utils/PdfUtils.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java index 000a8294..7ae91cf9 100644 --- a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java @@ -203,7 +203,6 @@ public class PdfUtils { try (PDDocument document = PDDocument.load(new ByteArrayInputStream(inputStream))) { PDFRenderer pdfRenderer = new PDFRenderer(document); int pageCount = document.getNumberOfPages(); - List images = new ArrayList<>(); // Create a ByteArrayOutputStream to save the image(s) to ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -232,7 +231,8 @@ public class PdfUtils { writer.dispose(); } else { // Combine all images into a single big image - BufferedImage combined = new BufferedImage(images.get(0).getWidth(), images.get(0).getHeight() * pageCount, BufferedImage.TYPE_INT_RGB); + BufferedImage image = pdfRenderer.renderImageWithDPI(0, DPI, colorType); + BufferedImage combined = new BufferedImage(image.getWidth(), image.getHeight() * pageCount, BufferedImage.TYPE_INT_RGB); Graphics g = combined.getGraphics(); for (int i = 0; i < pageCount; ++i) { From d405b7a810dcf973ddbce5819d2e239b9105b152 Mon Sep 17 00:00:00 2001 From: sbplat <71648843+sbplat@users.noreply.github.com> Date: Wed, 27 Dec 2023 22:46:55 -0500 Subject: [PATCH 3/4] perf: avoid re-rendering the first pdf page --- src/main/java/stirling/software/SPDF/utils/PdfUtils.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java index 7ae91cf9..2be68d08 100644 --- a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java @@ -236,7 +236,9 @@ public class PdfUtils { Graphics g = combined.getGraphics(); for (int i = 0; i < pageCount; ++i) { - BufferedImage image = pdfRenderer.renderImageWithDPI(i, DPI, colorType); + if (i != 0) { + image = pdfRenderer.renderImageWithDPI(i, DPI, colorType); + } g.drawImage(image, 0, i * image.getHeight(), null); } From c3f501d7018ae182953e6c16419c04fae38fb2b0 Mon Sep 17 00:00:00 2001 From: Ikko Eltociear Ashimine Date: Fri, 29 Dec 2023 00:34:56 +0900 Subject: [PATCH 4/4] chore: update README.md a -> an --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ad6f04c6..5a37bb30 100644 --- a/README.md +++ b/README.md @@ -267,7 +267,7 @@ For API usage you must provide a header with 'X-API-Key' and the associated API - Fill forms mannual and automatic ### Q2: Why is my application downloading .htm files? -This is a issue caused commonly by your NGINX configuration. The default file upload size for NGINX is 1MB, you need to add the following in your Nginx sites-available file. ``client_max_body_size SIZE;`` Where "SIZE" is 50M for example for 50MB files. +This is an issue caused commonly by your NGINX configuration. The default file upload size for NGINX is 1MB, you need to add the following in your Nginx sites-available file. ``client_max_body_size SIZE;`` Where "SIZE" is 50M for example for 50MB files. ### Q3: Why is my download timing out NGINX has timeout values by default so if you are running Stirling-PDF behind NGINX you may need to set a timeout value such as adding the config ``proxy_read_timeout 3600;``