Example usage for org.apache.pdfbox.preflight ValidationResult getErrorsList

List of usage examples for org.apache.pdfbox.preflight ValidationResult getErrorsList

Introduction

In this page you can find the example usage for org.apache.pdfbox.preflight ValidationResult getErrorsList.

Prototype

public List<ValidationError> getErrorsList() 

Source Link

Usage

From source file:at.gv.egiz.pdfas.lib.impl.signing.pdfbox2.PADESPDFBOXSigner.java

License:EUPL

private void runPDFAPreflight(final DataSource signedDocument) throws PdfAsException {
    PreflightDocument document = null;/*  ww w. ja va 2 s. com*/
    ValidationResult result = null;
    try {
        PreflightParser parser = new PreflightParser(signedDocument);
        parser.parse(Format.PDF_A1B);
        parser.parse();
        document = parser.getPreflightDocument();
        document.validate();

        document.close();
        result = document.getResult();

        if (result.getErrorsList().size() > 0) {
            logger.error("The following validation errors occured for PDF-A validation");
        }

        for (ValidationResult.ValidationError ve : result.getErrorsList()) {
            logger.error("\t" + ve.getErrorCode() + ": " + ve.getDetails());
        }

        if (!result.isValid()) {
            throw new PdfAsException("The file is not a valid PDF-A document");
        }

    } catch (SyntaxValidationException e) {
        logger.error("The file is syntactically invalid.", e);
        throw new PdfAsException("Resulting PDF Document is syntactically invalid.");
    } catch (ValidationException e) {
        logger.error("The file is not a valid PDF-A document.", e);
        throw new PdfAsException("The file is not a valid PDF-A document");
    } catch (IOException e) {
        logger.error("An IOException (" + e.getMessage() + ") occurred, while validating the PDF-A conformance",
                e);
        throw new PdfAsException("Failed validating PDF Document IOException.");
    } catch (RuntimeException e) {
        logger.debug(
                "An RuntimeException (" + e.getMessage() + ") occurred, while validating the PDF-A conformance",
                e);
        throw new PdfAsException("Failed validating PDF Document RuntimeException.");
    } finally {
        if (document != null) {
            IOUtils.closeQuietly((Closeable) document);
        }
    }
}

From source file:de.uzk.hki.da.convert.PdfService.java

License:Open Source License

/**
 * The Apache Preflight library is a Java tool that implements a parser
 * compliant with the ISO-19005 specification (aka PDF/A-1). Check
 * Compliance with PDF/A-1b//from   ww w . ja  v a2 s . c o m
 *
 * @param file the file
 * @return true, if successful
 * @Author: Jens Peters
 */

public static boolean validatePdfA(File file) {

    ValidationResult result = null;
    try {

        FileDataSource fd = new FileDataSource(file);
        PreflightParser parser;
        parser = new PreflightParser(fd);
        parser.parse();
        PreflightDocument document = parser.getPreflightDocument();
        document.validate();
        result = document.getResult();
        document.close();
    } catch (Exception e) {
        logger.error("Exception validating PDF/A compliance for " + file + " " + e.getCause());
        return false;
    }
    if (result.isValid()) {
        logger.info("The file " + file + " is a valid PDF/A-1b file");
        return true;
    } else {
        logger.info("The file" + file + " is not a valid PDF/A-1b, error(s) :");
        for (ValidationError error : result.getErrorsList()) {
            logger.info(error.getErrorCode() + " : " + error.getDetails());
        }
    }
    return false;
}

From source file:fi.vm.sade.eperusteet.service.dokumentti.impl.DokumenttiServiceImpl.java

License:EUPL

private byte[] generateFor(DokumenttiDto dto)
        throws IOException, TransformerException, ParserConfigurationException, SAXException {

    Peruste peruste = perusteRepository.findOne(dto.getPerusteId());
    Kieli kieli = dto.getKieli();//from ww w . jav a 2  s  .  c o m
    Dokumentti dokumentti = mapper.map(dto, Dokumentti.class);
    byte[] toReturn = null;
    ValidationResult result;
    GeneratorVersion version = dto.getGeneratorVersion();

    DokumenttiMetaDto meta = DokumenttiMetaDto.builder()
            .title(DokumenttiUtils.getTextString(dokumentti.getKieli(), peruste.getNimi())).build();

    log.info("Luodaan dokumenttia (" + dto.getPerusteId() + ", " + dto.getSuoritustapakoodi() + ", " + kieli
            + ", " + version + ") perusteelle.");
    switch (version) {
    case VANHA:
        throw new BusinessRuleViolationException("vanha-generointi-poistettu-kaytosta");
    case UUSI:
        Document doc = newBuilder.generateXML(peruste, dokumentti);

        meta.setSubject(messages.translate("docgen.meta.subject.peruste", kieli));
        toReturn = pdfService.xhtml2pdf(doc, meta);

        // Validoidaan dokumnetti
        result = DokumenttiUtils.validatePdf(toReturn);
        if (result.isValid()) {
            log.debug("Dokumentti (" + dto.getPerusteId() + ", " + dto.getSuoritustapakoodi() + ", " + kieli
                    + ") on PDF/A-1b mukainen.");
        } else {
            log.debug("Dokumentti (" + dto.getPerusteId() + ", " + dto.getSuoritustapakoodi() + ", " + kieli
                    + ") ei ole PDF/A-1b mukainen. Dokumentti sislt virheen/virheet:");
            result.getErrorsList().forEach(
                    error -> log.debug("  - " + error.getDetails() + " (" + error.getErrorCode() + ")"));
        }

        break;
    case KVLIITE:
        doc = kvLiiteBuilderService.generateXML(peruste, kieli);

        meta.setSubject(messages.translate("docgen.meta.subject.kvliite", kieli));
        toReturn = pdfService.xhtml2pdf(doc, version, meta);

        // Validoi kvliite
        result = DokumenttiUtils.validatePdf(toReturn);
        if (result.isValid()) {
            log.debug("Dokumentti (" + dto.getPerusteId() + ", " + kieli + ") on PDF/A-1b mukainen.");
        } else {
            log.debug("Dokumentti (" + dto.getId() + ", " + kieli
                    + ") ei ole PDF/A-1b mukainen. Dokumentti sislt virheen/virheet:");
            result.getErrorsList().forEach(
                    error -> log.debug("  - " + error.getDetails() + " (" + error.getErrorCode() + ")"));
        }

        break;
    default:
        break;
    }
    return toReturn;
}

From source file:uk.bl.wa.parsers.ApachePreflightParser.java

License:Open Source License

@Override
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
        throws IOException, SAXException, TikaException {

    // Attempt to reduce logging of stacktraces:
    //System.setProperty("log4j.logger.org.apache.pdfbox","");

    // Set up the validation result:
    ValidationResult result = null;

    InputStreamDataSource isds = new InputStreamDataSource(stream);
    PreflightParser parser = new PreflightParser(isds);
    PreflightDocument document = null;//from  w w  w. j  a v a2 s . co m
    try {

        /* Parse the PDF file with PreflightParser that inherits from the NonSequentialParser.
         * Some additional controls are present to check a set of PDF/A requirements. 
         * (Stream length consistency, EOL after some Keyword...)
         */
        parser.parse();

        /* Once the syntax validation is done, 
         * the parser can provide a PreflightDocument 
         * (that inherits from PDDocument) 
         * This document process the end of PDF/A validation.
         */
        document = parser.getPreflightDocument();
        document.validate();

        // Get validation result
        result = document.getResult();

    } catch (SyntaxValidationException e) {
        /*
         * the parse method can throw a SyntaxValidationExceptionif the PDF
         * file can't be parsed.
         * 
         * In this case, the exception contains an instance of
         * ValidationResult
         */
        result = e.getResult();
    } catch (Exception e) {
        // Otherwise, a NULL result:
        result = null;

    } finally {
        // Ensure the document is always closed:
        if (document != null)
            document.close();
    }

    // display validation result
    Set<String> rs = new HashSet<String>();
    if (result != null && result.isValid()) {
        //System.out.println("The resource is not a valid PDF/A-1b file");
        metadata.set(PDF_PREFLIGHT_VALID, Boolean.TRUE.toString());
    } else {
        //System.out.println("The resource is not valid, error(s) :");
        metadata.set(PDF_PREFLIGHT_VALID, Boolean.FALSE.toString());
        if (result != null) {
            for (ValidationError error : result.getErrorsList()) {
                // System.out.println(error.getErrorCode() + " : " +
                // error.getDetails());
                rs.add(error.getErrorCode() + " : " + error.getDetails());
            }
        }
    }

    metadata.set(PDF_PREFLIGHT_ERRORS, rs.toArray(new String[] {}));

}