This commit is contained in:
Anthony Stirling 2025-07-02 11:58:29 +01:00
parent 2148556f94
commit c9c44723c2
3 changed files with 31 additions and 39 deletions

View File

@ -311,7 +311,7 @@ public class ExceptionUtils {
public static void logException(String operation, Exception e) { public static void logException(String operation, Exception e) {
if (e instanceof IOException && PdfErrorUtils.isCorruptedPdfError((IOException) e)) { if (e instanceof IOException && PdfErrorUtils.isCorruptedPdfError((IOException) e)) {
log.warn("PDF corruption detected during {}: {}", operation, e.getMessage()); log.warn("PDF corruption detected during {}: {}", operation, e.getMessage());
} else if (isEncryptionError((IOException) e) || isPasswordError((IOException) e)) { } else if (e instanceof IOException io && (isEncryptionError(io) || isPasswordError(io))) {
log.info("PDF security issue during {}: {}", operation, e.getMessage()); log.info("PDF security issue during {}: {}", operation, e.getMessage());
} else { } else {
log.error("Unexpected error during {}", operation, e); log.error("Unexpected error during {}", operation, e);

View File

@ -139,12 +139,10 @@ public class PdfUtils {
// Validate and limit DPI to prevent excessive memory usage // Validate and limit DPI to prevent excessive memory usage
final int MAX_SAFE_DPI = 300; // Maximum safe DPI to prevent memory issues final int MAX_SAFE_DPI = 300; // Maximum safe DPI to prevent memory issues
if (DPI > MAX_SAFE_DPI) { if (DPI > MAX_SAFE_DPI) {
throw new IllegalArgumentException( throw ExceptionUtils.createIllegalArgumentException(
String.format( "error.dpiExceedsLimit",
"DPI value %d exceeds maximum safe limit of %d. " "DPI value {0} exceeds maximum safe limit of {1}. High DPI values can cause memory issues and crashes. Please use a lower DPI value.",
+ "High DPI values can cause memory issues and crashes. " DPI, MAX_SAFE_DPI);
+ "Please use a lower DPI value.",
DPI, MAX_SAFE_DPI));
} }
try (PDDocument document = pdfDocumentFactory.load(inputStream)) { try (PDDocument document = pdfDocumentFactory.load(inputStream)) {
@ -177,12 +175,10 @@ public class PdfUtils {
if (e.getMessage() != null if (e.getMessage() != null
&& e.getMessage() && e.getMessage()
.contains("Maximum size of image exceeded")) { .contains("Maximum size of image exceeded")) {
throw new IllegalArgumentException( throw ExceptionUtils.createIllegalArgumentException(
String.format( "error.pageTooBigForDpi",
"PDF page %d is too large to render at %d DPI. " "PDF page {0} is too large to render at {1} DPI. Please try a lower DPI value (recommended: 150 or less).",
+ "Please try a lower DPI value (recommended: 150 or less).", i + 1, DPI);
i + 1, DPI),
e);
} }
throw e; throw e;
} }
@ -223,13 +219,10 @@ public class PdfUtils {
if (e.getMessage() != null if (e.getMessage() != null
&& e.getMessage() && e.getMessage()
.contains("Maximum size of image exceeded")) { .contains("Maximum size of image exceeded")) {
throw new IllegalArgumentException( throw ExceptionUtils.createIllegalArgumentException(
String.format( "error.pageTooBigExceedsArray",
"PDF page %d is too large to render at %d DPI. " "PDF page {0} is too large to render at {1} DPI. The resulting image would exceed Java's maximum array size. Please try a lower DPI value (recommended: 150 or less).",
+ "The resulting image would exceed Java's maximum array size. " i + 1, DPI);
+ "Please try a lower DPI value (recommended: 150 or less).",
i + 1, DPI),
e);
} }
throw e; throw e;
} }
@ -266,12 +259,10 @@ public class PdfUtils {
if (e.getMessage() != null if (e.getMessage() != null
&& e.getMessage() && e.getMessage()
.contains("Maximum size of image exceeded")) { .contains("Maximum size of image exceeded")) {
throw new IllegalArgumentException( throw ExceptionUtils.createIllegalArgumentException(
String.format( "error.pageTooBigForDpi",
"PDF page %d is too large to render at %d DPI. " "PDF page {0} is too large to render at {1} DPI. Please try a lower DPI value (recommended: 150 or less).",
+ "Please try a lower DPI value (recommended: 150 or less).", i + 1, DPI);
i + 1, DPI),
e);
} }
throw e; throw e;
} }
@ -300,12 +291,10 @@ public class PdfUtils {
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
if (e.getMessage() != null if (e.getMessage() != null
&& e.getMessage().contains("Maximum size of image exceeded")) { && e.getMessage().contains("Maximum size of image exceeded")) {
throw new IllegalArgumentException( throw ExceptionUtils.createIllegalArgumentException(
String.format( "error.pageTooBigForDpi",
"PDF page %d is too large to render at %d DPI. " "PDF page {0} is too large to render at {1} DPI. Please try a lower DPI value (recommended: 150 or less).",
+ "Please try a lower DPI value (recommended: 150 or less).", i + 1, DPI);
i + 1, DPI),
e);
} }
throw e; throw e;
} }
@ -352,13 +341,10 @@ public class PdfUtils {
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
if (e.getMessage() != null if (e.getMessage() != null
&& e.getMessage().contains("Maximum size of image exceeded")) { && e.getMessage().contains("Maximum size of image exceeded")) {
throw new IllegalArgumentException( throw ExceptionUtils.createIllegalArgumentException(
String.format( "error.pageTooBigFor300Dpi",
"PDF page %d is too large to render at 300 DPI. " "PDF page {0} is too large to render at 300 DPI. The resulting image would exceed Java's maximum array size. Please use a lower DPI value for PDF-to-image conversion.",
+ "The resulting image would exceed Java's maximum array size. " page + 1);
+ "Please use a lower DPI value for PDF-to-image conversion.",
page + 1),
e);
} }
throw e; throw e;
} }

View File

@ -194,6 +194,12 @@ error.invalidFormat=Invalid {0} format: {1}
error.endpointDisabled=This endpoint has been disabled by the admin error.endpointDisabled=This endpoint has been disabled by the admin
error.urlNotReachable=URL is not reachable, please provide a valid URL error.urlNotReachable=URL is not reachable, please provide a valid URL
# DPI and image rendering messages
error.dpiExceedsLimit=DPI value {0} exceeds maximum safe limit of {1}. High DPI values can cause memory issues and crashes. Please use a lower DPI value.
error.pageTooBigForDpi=PDF page {0} is too large to render at {1} DPI. Please try a lower DPI value (recommended: 150 or less).
error.pageTooBigExceedsArray=PDF page {0} is too large to render at {1} DPI. The resulting image would exceed Java's maximum array size. Please try a lower DPI value (recommended: 150 or less).
error.pageTooBigFor300Dpi=PDF page {0} is too large to render at 300 DPI. The resulting image would exceed Java's maximum array size. Please use a lower DPI value for PDF-to-image conversion.
# URL and website conversion messages # URL and website conversion messages
# System requirements messages # System requirements messages