From 3a6c0c7722368c3f384fe83f695c1868917d01de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= <127139797+balazs-szucs@users.noreply.github.com> Date: Thu, 16 Oct 2025 23:50:04 +0200 Subject: [PATCH] feat(flatten): Add support for configuring rendering DPI in Flatten PDF feature (#4669) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description of Changes Rendering on high DPI can needlessly increase the file size and processing time, even when users would prefer smaller size, (with lesser quality) This PR adds support for configurable DPI, so that users can choose their preferred balance between processing time/file size and quality. **Backend logic and data model updates:** * The `FlattenController` now processes an optional `renderDpi` parameter from the request, using it to set the rendering DPI for each page. It enforces minimum and maximum values based on system configuration and ensures a sensible default if not provided. * The `FlattenRequest` model has a new `renderDpi` field to carry the requested DPI through the API and backend logic. **User interface and documentation improvements:** * The flatten form in `flatten.html` includes a new input field for DPI, with validation and help text to guide users about its impact and usage. * English resource messages have been updated to describe the new DPI field and provide help text explaining its effects. **UI:** image **File size before after (100 DPI vs 500 DPI):** image Sample used was originally 8 MB. Sample used are attached here: [image-doc-1.pdf](https://github.com/user-attachments/files/22889873/image-doc-1.pdf) [image-doc-2.pdf](https://github.com/user-attachments/files/22889876/image-doc-2.pdf) Strange stuff, I can personally _barely_ tell difference between 500 vs 100 DPI tbh, but the file size difference is very significant. Closes: #4668 --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing) for more details. --------- Signed-off-by: Balázs Szücs --- .../api/misc/FlattenController.java | 29 ++++++++++++++----- .../SPDF/model/api/misc/FlattenRequest.java | 6 ++++ .../main/resources/messages_en_GB.properties | 2 ++ .../resources/templates/misc/flatten.html | 15 +++++++++- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/FlattenController.java b/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/FlattenController.java index a76a737b56..6a187936ce 100644 --- a/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/FlattenController.java +++ b/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/FlattenController.java @@ -67,19 +67,32 @@ public class FlattenController { PDFRenderer pdfRenderer = new PDFRenderer(document); PDDocument newDocument = pdfDocumentFactory.createNewDocumentBasedOnOldDocument(document); + + int defaultRenderDpi = 100; // Default fallback + ApplicationProperties properties = + ApplicationContextProvider.getBean(ApplicationProperties.class); + Integer configuredMaxDpi = null; + if (properties != null && properties.getSystem() != null) { + configuredMaxDpi = properties.getSystem().getMaxDPI(); + } + + int maxDpi = + (configuredMaxDpi != null && configuredMaxDpi > 0) + ? configuredMaxDpi + : defaultRenderDpi; + + Integer requestedDpi = request.getRenderDpi(); + int renderDpi = maxDpi; + if (requestedDpi != null) { + renderDpi = Math.min(requestedDpi, maxDpi); + renderDpi = Math.max(renderDpi, 72); + } + int numPages = document.getNumberOfPages(); for (int i = 0; i < numPages; i++) { try { BufferedImage image; - // Use global maximum DPI setting, fallback to 300 if not set - int renderDpi = 300; // Default fallback - ApplicationProperties properties = - ApplicationContextProvider.getBean(ApplicationProperties.class); - if (properties != null && properties.getSystem() != null) { - renderDpi = properties.getSystem().getMaxDPI(); - } - try { image = pdfRenderer.renderImageWithDPI(i, renderDpi, ImageType.RGB); } catch (OutOfMemoryError e) { diff --git a/app/core/src/main/java/stirling/software/SPDF/model/api/misc/FlattenRequest.java b/app/core/src/main/java/stirling/software/SPDF/model/api/misc/FlattenRequest.java index cf4e7c575f..b53653a36f 100644 --- a/app/core/src/main/java/stirling/software/SPDF/model/api/misc/FlattenRequest.java +++ b/app/core/src/main/java/stirling/software/SPDF/model/api/misc/FlattenRequest.java @@ -18,4 +18,10 @@ public class FlattenRequest extends PDFFile { requiredMode = Schema.RequiredMode.REQUIRED, defaultValue = "false") private Boolean flattenOnlyForms; + + @Schema( + description = "Optional DPI for page rendering when flattening the full document.", + requiredMode = Schema.RequiredMode.NOT_REQUIRED, + minimum = "72") + private Integer renderDpi; } diff --git a/app/core/src/main/resources/messages_en_GB.properties b/app/core/src/main/resources/messages_en_GB.properties index 86f6e928ab..eb75856d3b 100644 --- a/app/core/src/main/resources/messages_en_GB.properties +++ b/app/core/src/main/resources/messages_en_GB.properties @@ -1259,6 +1259,8 @@ repair.submit=Repair flatten.title=Flatten flatten.header=Flatten PDF flatten.flattenOnlyForms=Flatten only forms +flatten.renderDpi=Rendering DPI (optional, recommended 150 DPI): +flatten.renderDpi.help=Leave blank to use the system default. Higher DPI sharpens output but increases processing time and file size. flatten.submit=Flatten diff --git a/app/core/src/main/resources/templates/misc/flatten.html b/app/core/src/main/resources/templates/misc/flatten.html index fca06e0931..3e31d1cbf9 100644 --- a/app/core/src/main/resources/templates/misc/flatten.html +++ b/app/core/src/main/resources/templates/misc/flatten.html @@ -24,7 +24,20 @@
-
+ +
+ + + +