mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-11-16 01:21:16 +01:00
refactor(pdf-conversion): modularize PDF/A preflight parsing and improve resource path handling
- 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 <bszucs1209@gmail.com>
This commit is contained in:
parent
4514c06838
commit
796f739fde
@ -184,6 +184,21 @@ public class ConvertPDFToPDFA {
|
|||||||
return command;
|
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 {
|
private static void validatePdfaOutput(Path pdfPath, PdfaProfile profile) throws IOException {
|
||||||
Optional<Format> format = profile.preflightFormat();
|
Optional<Format> format = profile.preflightFormat();
|
||||||
if (format.isEmpty()) {
|
if (format.isEmpty()) {
|
||||||
@ -194,22 +209,7 @@ public class ConvertPDFToPDFA {
|
|||||||
try (RandomAccessRead rar = new RandomAccessReadBufferedFile(pdfPath.toFile())) {
|
try (RandomAccessRead rar = new RandomAccessReadBufferedFile(pdfPath.toFile())) {
|
||||||
PreflightParser parser = new PreflightParser(rar);
|
PreflightParser parser = new PreflightParser(rar);
|
||||||
|
|
||||||
PreflightDocument document;
|
PreflightDocument document = parsePreflightDocument(parser, format.get(), profile);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (document == null) {
|
if (document == null) {
|
||||||
throw new IOException(
|
throw new IOException(
|
||||||
"PDF/A preflight returned no document for " + profile.getDisplayName());
|
"PDF/A preflight returned no document for " + profile.getDisplayName());
|
||||||
@ -928,8 +928,8 @@ public class ConvertPDFToPDFA {
|
|||||||
ZonedDateTime creationZdt = ZonedDateTime.ofInstant(creationInstant, ZoneId.of("UTC"));
|
ZonedDateTime creationZdt = ZonedDateTime.ofInstant(creationInstant, ZoneId.of("UTC"));
|
||||||
|
|
||||||
// Convert to GregorianCalendar for PDFBox API compatibility
|
// Convert to GregorianCalendar for PDFBox API compatibility
|
||||||
GregorianCalendar creationCal = java.util.GregorianCalendar.from(creationZdt);
|
GregorianCalendar creationCal = GregorianCalendar.from(creationZdt);
|
||||||
GregorianCalendar modificationCal = java.util.GregorianCalendar.from(nowZdt);
|
GregorianCalendar modificationCal = GregorianCalendar.from(nowZdt);
|
||||||
|
|
||||||
docInfo.setCreationDate(creationCal);
|
docInfo.setCreationDate(creationCal);
|
||||||
xmpBasicSchema.setCreateDate(creationCal);
|
xmpBasicSchema.setCreateDate(creationCal);
|
||||||
@ -1152,12 +1152,10 @@ public class ConvertPDFToPDFA {
|
|||||||
|
|
||||||
private void addICCProfileIfNotPresent(PDDocument document) {
|
private void addICCProfileIfNotPresent(PDDocument document) {
|
||||||
if (document.getDocumentCatalog().getOutputIntents().isEmpty()) {
|
if (document.getDocumentCatalog().getOutputIntents().isEmpty()) {
|
||||||
try (InputStream colorProfile = getClass().getResourceAsStream("/icc/sRGB2014.icc")) {
|
try (InputStream colorProfile = getClass().getResourceAsStream(ICC_RESOURCE_PATH)) {
|
||||||
if (colorProfile == null) {
|
if (colorProfile == null) {
|
||||||
throw ExceptionUtils.createIllegalArgumentException(
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
"error.resourceNotFound",
|
"error.resourceNotFound", "Resource not found: {0}", ICC_RESOURCE_PATH);
|
||||||
"Resource not found: {0}",
|
|
||||||
"/icc/sRGB2014.icc");
|
|
||||||
}
|
}
|
||||||
PDOutputIntent outputIntent = new PDOutputIntent(document, colorProfile);
|
PDOutputIntent outputIntent = new PDOutputIntent(document, colorProfile);
|
||||||
outputIntent.setInfo("sRGB IEC61966-2.1");
|
outputIntent.setInfo("sRGB IEC61966-2.1");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user