diff --git a/pom.xml b/pom.xml index cb74608..7fcdc36 100644 --- a/pom.xml +++ b/pom.xml @@ -282,5 +282,15 @@ webp-imageio 0.1.6 + + org.verapdf + validation-model + 1.26.1 + + + org.verapdf + validation-model-jakarta + 1.26.1 + diff --git a/src/main/java/org/metanorma/fop/PDFGenerator.java b/src/main/java/org/metanorma/fop/PDFGenerator.java index a6599cf..abd0b53 100644 --- a/src/main/java/org/metanorma/fop/PDFGenerator.java +++ b/src/main/java/org/metanorma/fop/PDFGenerator.java @@ -490,6 +490,11 @@ private void convertmn2pdf(fontConfig fontcfg, XSLTconverter xsltConverter, File logger.info(WARNING_NONPDFUA); } + // validate PDF by veraPDF + VeraPDFValidator veraPDFValidator = new VeraPDFValidator(); + veraPDFValidator.validate(pdf); + + fontcfg.printMessages(); } catch (Exception e) { diff --git a/src/main/java/org/metanorma/fop/VeraPDFValidator.java b/src/main/java/org/metanorma/fop/VeraPDFValidator.java new file mode 100644 index 0000000..2a82ee5 --- /dev/null +++ b/src/main/java/org/metanorma/fop/VeraPDFValidator.java @@ -0,0 +1,48 @@ +package org.metanorma.fop; + +import org.metanorma.utils.LoggerHelper; +import org.verapdf.core.EncryptedPdfException; +import org.verapdf.core.ModelParsingException; +import org.verapdf.core.ValidationException; +import org.verapdf.gf.foundry.VeraGreenfieldFoundryProvider; +import org.verapdf.pdfa.Foundries; +import org.verapdf.pdfa.PDFAParser; +import org.verapdf.pdfa.results.ValidationResult; +import org.verapdf.pdfa.PDFAValidator; +import org.verapdf.pdfa.flavours.PDFAFlavour; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.logging.Logger; + +import static org.metanorma.Constants.PDF_A_MODE; + +public class VeraPDFValidator { + + protected static final Logger logger = Logger.getLogger(LoggerHelper.LOGGER_NAME); + + // see https://docs.verapdf.org/develop/ + public void validate(File filePDF) { + VeraGreenfieldFoundryProvider.initialise(); + String sPDFAmode = PDF_A_MODE.substring(PDF_A_MODE.indexOf("-")); + PDFAFlavour flavour = PDFAFlavour.fromString(sPDFAmode); + try (PDFAParser parser = Foundries.defaultInstance().createParser(new FileInputStream(filePDF), flavour)) { + PDFAValidator validator = Foundries.defaultInstance().createValidator(flavour, false); + ValidationResult result = validator.validate(parser); + + if (result.isCompliant()) { + // File is a valid PDF/A PDF_A_MODE + return; + } else { + // it isn't + logger.severe("PDF isn't valid " + PDF_A_MODE + ":"); + logger.severe(result.toString()); + } + } catch (IOException | ValidationException | ModelParsingException | EncryptedPdfException exception) { + // Exception during validation + logger.severe(exception.toString()); + } + return; + } + +}