From 796f739fdee17fc260c1117e9cf9f260ec47b024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Sat, 8 Nov 2025 16:34:00 +0100 Subject: [PATCH] refactor(pdf-conversion): modularize PDF/A preflight parsing and improve resource path handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Extracted PDF/A preflight parsing logic into `parsePreflightDocument` for reusability - Replaced hardcoded ICC resource paths with constants for better maintainability - Simplified GregorianCalendar conversions by removing redundant `java.util` statement - Improved exception handling for preflight parsing with detailed error messages Signed-off-by: Balázs Szücs --- .../api/converters/ConvertPDFToPDFA.java | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 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 c8e63e3c7..13f368f30 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 @@ -184,6 +184,21 @@ public class ConvertPDFToPDFA { return command; } + private static PreflightDocument parsePreflightDocument( + PreflightParser parser, Format format, PdfaProfile profile) throws IOException { + try { + return (PreflightDocument) + parser.parse(format, PreflightConfiguration.createPdfA1BConfiguration()); + } catch (SyntaxValidationException e) { + throw new IOException(buildPreflightErrorMessage(e.getResult(), profile), e); + } catch (ClassCastException e) { + throw new IOException( + "PDF/A preflight did not produce a PreflightDocument for " + + profile.getDisplayName(), + e); + } + } + private static void validatePdfaOutput(Path pdfPath, PdfaProfile profile) throws IOException { Optional format = profile.preflightFormat(); if (format.isEmpty()) { @@ -194,22 +209,7 @@ public class ConvertPDFToPDFA { try (RandomAccessRead rar = new RandomAccessReadBufferedFile(pdfPath.toFile())) { PreflightParser parser = new PreflightParser(rar); - PreflightDocument document; - try { - document = - (PreflightDocument) - parser.parse( - format.get(), - PreflightConfiguration.createPdfA1BConfiguration()); - } catch (SyntaxValidationException e) { - throw new IOException(buildPreflightErrorMessage(e.getResult(), profile), e); - } catch (ClassCastException e) { - throw new IOException( - "PDF/A preflight did not produce a PreflightDocument for " - + profile.getDisplayName(), - e); - } - + PreflightDocument document = parsePreflightDocument(parser, format.get(), profile); if (document == null) { throw new IOException( "PDF/A preflight returned no document for " + profile.getDisplayName()); @@ -928,8 +928,8 @@ public class ConvertPDFToPDFA { ZonedDateTime creationZdt = ZonedDateTime.ofInstant(creationInstant, ZoneId.of("UTC")); // Convert to GregorianCalendar for PDFBox API compatibility - GregorianCalendar creationCal = java.util.GregorianCalendar.from(creationZdt); - GregorianCalendar modificationCal = java.util.GregorianCalendar.from(nowZdt); + GregorianCalendar creationCal = GregorianCalendar.from(creationZdt); + GregorianCalendar modificationCal = GregorianCalendar.from(nowZdt); docInfo.setCreationDate(creationCal); xmpBasicSchema.setCreateDate(creationCal); @@ -1152,12 +1152,10 @@ public class ConvertPDFToPDFA { private void addICCProfileIfNotPresent(PDDocument document) { if (document.getDocumentCatalog().getOutputIntents().isEmpty()) { - try (InputStream colorProfile = getClass().getResourceAsStream("/icc/sRGB2014.icc")) { + try (InputStream colorProfile = getClass().getResourceAsStream(ICC_RESOURCE_PATH)) { if (colorProfile == null) { throw ExceptionUtils.createIllegalArgumentException( - "error.resourceNotFound", - "Resource not found: {0}", - "/icc/sRGB2014.icc"); + "error.resourceNotFound", "Resource not found: {0}", ICC_RESOURCE_PATH); } PDOutputIntent outputIntent = new PDOutputIntent(document, colorProfile); outputIntent.setInfo("sRGB IEC61966-2.1");