From 69140b4b03bab2c6e25d37610cc064bbee5e8e05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Sat, 8 Nov 2025 12:15:19 +0100 Subject: [PATCH] refactor(pdf-conversion): improve request token handling and resource validation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added null checks for PDResources to prevent possible null pointer exceptions - Streamlined request token processing by normalizing input directly in initialization - Simplified filter logic for profile matching by leveraging pre-normalized tokens - Removed redundant option from the PDF/A format dropdown in the UI template Signed-off-by: Balázs Szücs --- .../api/converters/ConvertPDFToPDFA.java | 24 +++++++++---------- .../templates/convert/pdf-to-pdfa.html | 1 - 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/app/core/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToPDFA.java b/app/core/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToPDFA.java index 022f5fe61..ae3205ae7 100644 --- a/app/core/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToPDFA.java +++ b/app/core/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToPDFA.java @@ -537,6 +537,8 @@ public class ConvertPDFToPDFA { PDResources loRes = loPage.getResources(); PDResources baseRes = basePage.getResources(); + if (loRes == null || baseRes == null) continue; + Set toReplace = detectTransparentXObjects(basePage); for (COSName name : toReplace) { @@ -1189,7 +1191,10 @@ public class ConvertPDFToPDFA { this.suffix = suffix; this.compatibilityLevel = compatibilityLevel; this.preflightFormat = preflightFormat; - this.requestTokens = Arrays.asList(requestTokens); + this.requestTokens = + Arrays.stream(requestTokens) + .map(token -> token.toLowerCase(Locale.ROOT)) + .toList(); } static PdfaProfile fromRequest(String requestToken) { @@ -1199,11 +1204,7 @@ public class ConvertPDFToPDFA { String normalized = requestToken.trim().toLowerCase(Locale.ROOT); Optional match = Arrays.stream(values()) - .filter( - profile -> - profile.requestTokens.stream() - .map(token -> token.toLowerCase(Locale.ROOT)) - .anyMatch(token -> token.equals(normalized))) + .filter(profile -> profile.requestTokens.contains(normalized)) .findFirst(); return match.orElse(PDF_A_2B); @@ -1251,7 +1252,10 @@ public class ConvertPDFToPDFA { this.suffix = suffix; this.compatibilityLevel = compatibilityLevel; this.pdfxVersion = pdfxVersion; - this.requestTokens = Arrays.asList(requestTokens); + this.requestTokens = + Arrays.stream(requestTokens) + .map(token -> token.toLowerCase(Locale.ROOT)) + .toList(); } static PdfXProfile fromRequest(String requestToken) { @@ -1261,11 +1265,7 @@ public class ConvertPDFToPDFA { String normalized = requestToken.trim().toLowerCase(Locale.ROOT); Optional match = Arrays.stream(values()) - .filter( - profile -> - profile.requestTokens.stream() - .map(token -> token.toLowerCase(Locale.ROOT)) - .anyMatch(token -> token.equals(normalized))) + .filter(profile -> profile.requestTokens.contains(normalized)) .findFirst(); return match.orElse(PDF_X_4); diff --git a/app/core/src/main/resources/templates/convert/pdf-to-pdfa.html b/app/core/src/main/resources/templates/convert/pdf-to-pdfa.html index 2e9ed11ef..5b44ff5b6 100644 --- a/app/core/src/main/resources/templates/convert/pdf-to-pdfa.html +++ b/app/core/src/main/resources/templates/convert/pdf-to-pdfa.html @@ -25,7 +25,6 @@