Skip to content

Commit

Permalink
veraPDF validation feature added, #265
Browse files Browse the repository at this point in the history
  • Loading branch information
Intelligent2013 committed Sep 2, 2024
1 parent 08b0ebb commit 3cd4762
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -282,5 +282,15 @@
<artifactId>webp-imageio</artifactId>
<version>0.1.6</version>
</dependency>
<dependency>
<groupId>org.verapdf</groupId>
<artifactId>validation-model</artifactId>
<version>1.26.1</version>
</dependency>
<dependency>
<groupId>org.verapdf</groupId>
<artifactId>validation-model-jakarta</artifactId>
<version>1.26.1</version>
</dependency>
</dependencies>
</project>
5 changes: 5 additions & 0 deletions src/main/java/org/metanorma/fop/PDFGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/metanorma/fop/VeraPDFValidator.java
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 3cd4762

Please sign in to comment.