Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

Treat maxDPI as a ceiling rather than the render resolution (#7824)

Anthony Stirling1ba8c13e96a0
diff --git a/app/common/src/main/java/stirling/software/common/util/PdfUtils.java b/app/common/src/main/java/stirling/software/common/util/PdfUtils.java
index 89d9f6f1dc..90cacdf3b8 100644
--- a/app/common/src/main/java/stirling/software/common/util/PdfUtils.java
+++ b/app/common/src/main/java/stirling/software/common/util/PdfUtils.java
@@ -47,6 +47,9 @@ import stirling.software.common.service.CustomPDFDocumentFactory;
@UtilityClass
public class PdfUtils {
+ // Print-quality raster for flattening a page to an image; capped by system maxDPI
+ private static final int PDF_TO_IMAGE_DPI = 300;
+
private final RegexPatternUtils patternCache = RegexPatternUtils.getInstance();
public PDRectangle textToPageSize(String size) {
@@ -370,12 +373,12 @@ public class PdfUtils {
final int pageIndex = page;
BufferedImage bim;
- // Use global maximum DPI setting, fallback to 300 if not set
- int renderDpi = 300; // Default fallback
+ // maxDPI is a safety ceiling for user-supplied values, not a target resolution
+ int renderDpi = PDF_TO_IMAGE_DPI;
ApplicationProperties properties =
ApplicationContextProvider.getBean(ApplicationProperties.class);
if (properties != null && properties.getSystem() != null) {
- renderDpi = properties.getSystem().getMaxDPI();
+ renderDpi = Math.min(renderDpi, properties.getSystem().getMaxDPI());
}
final int dpi = renderDpi;
diff --git a/app/common/src/main/java/stirling/software/common/util/misc/InvertFullColorStrategy.java b/app/common/src/main/java/stirling/software/common/util/misc/InvertFullColorStrategy.java
index 75782f8986..cd39648d06 100644
--- a/app/common/src/main/java/stirling/software/common/util/misc/InvertFullColorStrategy.java
+++ b/app/common/src/main/java/stirling/software/common/util/misc/InvertFullColorStrategy.java
@@ -30,6 +30,9 @@ import stirling.software.common.util.ExceptionUtils;
@Slf4j
public class InvertFullColorStrategy extends ReplaceAndInvertColorStrategy {
+ // Print-quality raster for the inverted page; capped by system maxDPI
+ private static final int INVERT_RENDER_DPI = 300;
+
public InvertFullColorStrategy(MultipartFile file, ReplaceAndInvert replaceAndInvert) {
super(file, replaceAndInvert);
}
@@ -52,12 +55,12 @@ public class InvertFullColorStrategy extends ReplaceAndInvertColorStrategy {
for (int page = 0; page < document.getNumberOfPages(); page++) {
BufferedImage image;
- // Use global maximum DPI setting, fallback to 300 if not set
- int renderDpi = 300; // Default fallback
+ // maxDPI is a safety ceiling for user-supplied values, not a target resolution
+ int renderDpi = INVERT_RENDER_DPI;
ApplicationProperties properties =
ApplicationContextProvider.getBean(ApplicationProperties.class);
if (properties != null && properties.getSystem() != null) {
- renderDpi = properties.getSystem().getMaxDPI();
+ renderDpi = Math.min(renderDpi, properties.getSystem().getMaxDPI());
}
final int dpi = renderDpi;
final int pageNum = page;
diff --git a/app/common/src/test/java/stirling/software/common/util/PdfUtilsMoreTest.java b/app/common/src/test/java/stirling/software/common/util/PdfUtilsMoreTest.java
index 6ca815e56c..668ba98902 100644
--- a/app/common/src/test/java/stirling/software/common/util/PdfUtilsMoreTest.java
+++ b/app/common/src/test/java/stirling/software/common/util/PdfUtilsMoreTest.java
@@ -153,6 +153,52 @@ class PdfUtilsMoreTest {
}
}
+ @Nested
+ @DisplayName("convertPdfToPdfImage render DPI")
+ class ConvertPdfToPdfImageDpi {
+
+ /** A one-inch-square page, so the embedded image's pixel width is the render DPI. */
+ private PDDocument oneInchPage() {
+ PDDocument doc = new PDDocument();
+ doc.addPage(new PDPage(new PDRectangle(72f, 72f)));
+ return doc;
+ }
+
+ private int embeddedImageWidth(PDDocument doc) throws IOException {
+ PDResources resources = doc.getPage(0).getResources();
+ for (var name : resources.getXObjectNames()) {
+ if (resources.isImageXObject(name)) {
+ return ((PDImageXObject) resources.getXObject(name)).getWidth();
+ }
+ }
+ throw new IllegalStateException("no image on page");
+ }
+
+ private int renderedWidthWithMaxDpi(int maxDpi) throws IOException {
+ try (PDDocument src = oneInchPage();
+ MockedStatic<ApplicationContextProvider> ctx =
+ Mockito.mockStatic(ApplicationContextProvider.class)) {
+ ctx.when(() -> ApplicationContextProvider.getBean(ApplicationProperties.class))
+ .thenReturn(propsWithMaxDpi(maxDpi));
+ try (PDDocument out = PdfUtils.convertPdfToPdfImage(src)) {
+ return embeddedImageWidth(out);
+ }
+ }
+ }
+
+ @Test
+ @DisplayName("a high maxDPI does not raise the render resolution above 300")
+ void capsAtPrintQuality() throws IOException {
+ assertThat(renderedWidthWithMaxDpi(500)).isEqualTo(300);
+ }
+
+ @Test
+ @DisplayName("a maxDPI below the target still clamps the render resolution")
+ void respectsLowerCeiling() throws IOException {
+ assertThat(renderedWidthWithMaxDpi(150)).isEqualTo(150);
+ }
+ }
+
// ---- convertFromPdf with ApplicationProperties present ------------------
@Nested
diff --git a/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/BlankPageController.java b/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/BlankPageController.java
index 8105f309bc..0a1477c72e 100644
--- a/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/BlankPageController.java
+++ b/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/BlankPageController.java
@@ -50,6 +50,10 @@ import stirling.software.common.util.WebResponseUtils;
@RequiredArgsConstructor
public class BlankPageController {
+ // Lowest resolution that classifies the same as 300-500 DPI. Below this a thin rule can
+ // antialias away and a page with content is dropped as blank.
+ private static final int BLANK_DETECTION_DPI = 150;
+
private final CustomPDFDocumentFactory pdfDocumentFactory;
private final TempFileManager tempFileManager;
@@ -130,12 +134,12 @@ public class BlankPageController {
// Render image and save as temp file
BufferedImage image;
- // Use global maximum DPI setting
- int renderDpi = 30; // Default fallback
+ // maxDPI is a safety ceiling for user-supplied values, not a target
+ int renderDpi = BLANK_DETECTION_DPI;
ApplicationProperties properties =
ApplicationContextProvider.getBean(ApplicationProperties.class);
if (properties != null && properties.getSystem() != null) {
- renderDpi = properties.getSystem().getMaxDPI();
+ renderDpi = Math.min(renderDpi, properties.getSystem().getMaxDPI());
}
final int dpi = renderDpi;
final int currentPageIndex = pageIndex;
diff --git a/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImageScansController.java b/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImageScansController.java
index 816b95d604..ef1202a6f9 100644
--- a/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImageScansController.java
+++ b/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImageScansController.java
@@ -53,6 +53,9 @@ import stirling.software.common.util.WebResponseUtils;
@RequiredArgsConstructor
public class ExtractImageScansController {
+ // Print-quality raster for the extracted scan; capped by system maxDPI
+ private static final int SCAN_RENDER_DPI = 300;
+
private static final String REPLACEFIRST = "[.][^.]+$";
private final CustomPDFDocumentFactory pdfDocumentFactory;
@@ -112,12 +115,12 @@ public class ExtractImageScansController {
// Render image and save as temp file
BufferedImage image;
- // Use global maximum DPI setting, fallback to 300 if not set
- int renderDpi = 300; // Default fallback
+ // maxDPI is a safety ceiling for user-supplied values, not a target
+ int renderDpi = SCAN_RENDER_DPI;
ApplicationProperties properties =
ApplicationContextProvider.getBean(ApplicationProperties.class);
if (properties != null && properties.getSystem() != null) {
- renderDpi = properties.getSystem().getMaxDPI();
+ renderDpi = Math.min(renderDpi, properties.getSystem().getMaxDPI());
}
final int dpi = renderDpi;
final int pageIndex = i;
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 9bbbeffccb..f88123117f 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
@@ -42,6 +42,9 @@ import stirling.software.common.util.WebResponseUtils;
@RequiredArgsConstructor
public class FlattenController {
+ // Default when the caller sends no renderDpi; an explicit request still wins up to maxDPI
+ private static final int FLATTEN_RENDER_DPI = 300;
+
private final CustomPDFDocumentFactory pdfDocumentFactory;
private final TempFileManager tempFileManager;
@@ -82,7 +85,6 @@ public class FlattenController {
try (PDDocument newDocument =
pdfDocumentFactory.createNewDocumentBasedOnOldDocument(document)) {
- int defaultRenderDpi = 100; // Default fallback
ApplicationProperties properties =
ApplicationContextProvider.getBean(ApplicationProperties.class);
Integer configuredMaxDpi = null;
@@ -93,10 +95,11 @@ public class FlattenController {
int maxDpi =
(configuredMaxDpi != null && configuredMaxDpi > 0)
? configuredMaxDpi
- : defaultRenderDpi;
+ : FLATTEN_RENDER_DPI;
Integer requestedDpi = request.getRenderDpi();
- int renderDpiTemp = maxDpi;
+ // maxDpi is a ceiling; without an explicit request use the print-quality target
+ int renderDpiTemp = Math.min(FLATTEN_RENDER_DPI, maxDpi);
if (requestedDpi != null) {
renderDpiTemp = Math.min(requestedDpi, maxDpi);
renderDpiTemp = Math.max(renderDpiTemp, 72);
diff --git a/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/OCRController.java b/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/OCRController.java
index e10b86a866..4bf9fb3d5b 100644
--- a/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/OCRController.java
+++ b/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/OCRController.java
@@ -61,6 +61,9 @@ import stirling.software.common.util.WebResponseUtils;
@RequiredArgsConstructor
public class OCRController {
+ // Tesseract's recommended minimum; more pixels cost time without improving recognition
+ private static final int OCR_RENDER_DPI = 300;
+
private final ApplicationProperties applicationProperties;
private final CustomPDFDocumentFactory pdfDocumentFactory;
private final TempFileManager tempFileManager;
@@ -392,11 +395,14 @@ public class OCRController {
// Convert page to image
BufferedImage image;
- // Use global maximum DPI setting, fallback to 300 if not set
- int renderDpi = 300; // Default fallback
+ // maxDPI is a safety ceiling for user-supplied values, not a target
+ int renderDpi = OCR_RENDER_DPI;
if (applicationProperties != null
&& applicationProperties.getSystem() != null) {
- renderDpi = applicationProperties.getSystem().getMaxDPI();
+ renderDpi =
+ Math.min(
+ renderDpi,
+ applicationProperties.getSystem().getMaxDPI());
}
final int dpi = renderDpi;
final int currentPageNum = pageNum;