Search in sources :

Example 1 with PreflightParser

use of org.apache.pdfbox.preflight.parser.PreflightParser in project pdfbox by apache.

the class Benchmark method main.

/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    if (args.length < 3) {
        System.err.println("Usage : Benchmark loop resultFile <file1 ... filen|dir>");
        System.exit(255);
    }
    Integer loop = Integer.parseInt(args[0]);
    FileWriter resFile = new FileWriter(new File(args[1]));
    List<File> lfd = new ArrayList<>();
    for (int i = 2; i < args.length; ++i) {
        File fi = new File(args[i]);
        if (fi.isDirectory()) {
            // Get All files contained by the dir
            Collection<File> cf = FileUtils.listFiles(fi, null, true);
            lfd.addAll(cf);
        } else {
            lfd.add(fi);
        }
    }
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.Z");
    long startGTime = System.currentTimeMillis();
    int size = lfd.size();
    for (int i = 0; i < loop; i++) {
        File file = lfd.get(i % size);
        long startLTime = System.currentTimeMillis();
        PreflightParser parser = new PreflightParser(file);
        parser.parse();
        PreflightDocument document = parser.getPreflightDocument();
        document.validate();
        ValidationResult result = document.getResult();
        if (!result.isValid()) {
            resFile.write(file.getAbsolutePath() + " isn't PDF/A\n");
            for (ValidationError error : result.getErrorsList()) {
                resFile.write(error.getErrorCode() + " : " + error.getDetails() + "\n");
            }
        }
        document.close();
        long endLTime = System.currentTimeMillis();
        resFile.write(file.getName() + " (ms) : " + (endLTime - startLTime) + "\n");
        resFile.flush();
    }
    long endGTime = System.currentTimeMillis();
    resFile.write("Start : " + sdf.format(new Date(startGTime)) + "\n");
    resFile.write("End : " + sdf.format(new Date(endGTime)) + "\n");
    resFile.write("Duration (ms) : " + (endGTime - startGTime) + "\n");
    resFile.write("Average (ms) : " + (int) ((endGTime - startGTime) / loop) + "\n");
    System.out.println("Start : " + sdf.format(new Date(startGTime)));
    System.out.println("End : " + sdf.format(new Date(endGTime)));
    System.out.println("Duration (ms) : " + (endGTime - startGTime));
    System.out.println("Average (ms) : " + (int) ((endGTime - startGTime) / loop));
    resFile.flush();
    IOUtils.closeQuietly(resFile);
}
Also used : FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) Date(java.util.Date) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser)

Example 2 with PreflightParser

use of org.apache.pdfbox.preflight.parser.PreflightParser in project pdfbox by apache.

the class AbstractInvalidFileTester method validate.

@Test()
public final void validate() throws Exception {
    if (path == null) {
        logger.warn("This is an empty test");
        return;
    }
    PreflightDocument document = null;
    try {
        PreflightParser parser = new PreflightParser(path);
        parser.parse();
        document = parser.getPreflightDocument();
        document.validate();
        ValidationResult result = document.getResult();
        Assert.assertFalse(path + " : Isartor file should be invalid (" + path + ")", result.isValid());
        Assert.assertTrue(path + " : Should find at least one error", result.getErrorsList().size() > 0);
        // could contain more than one error
        boolean found = false;
        if (this.expectedError != null) {
            for (ValidationError error : result.getErrorsList()) {
                if (error.getErrorCode().equals(this.expectedError)) {
                    found = true;
                    if (outputResult == null) {
                        break;
                    }
                }
                if (outputResult != null) {
                    String log = path.getName().replace(".pdf", "") + "#" + error.getErrorCode() + "#" + error.getDetails() + "\n";
                    outputResult.write(log.getBytes());
                }
            }
        }
        if (result.getErrorsList().size() > 0) {
            if (this.expectedError == null) {
                logger.info("File invalid as expected (no expected code) :" + this.path.getAbsolutePath());
            } else if (!found) {
                StringBuilder message = new StringBuilder(100);
                message.append(path).append(" : Invalid error code returned. Expected ");
                message.append(this.expectedError).append(", found ");
                for (ValidationError error : result.getErrorsList()) {
                    message.append(error.getErrorCode()).append(" ");
                }
                Assert.fail(message.toString());
            }
        } else {
            Assert.assertEquals(path + " : Invalid error code returned.", this.expectedError, result.getErrorsList().get(0).getErrorCode());
        }
    } catch (ValidationException e) {
        throw new Exception(path + " :" + e.getMessage(), e);
    } finally {
        if (document != null) {
            document.close();
        }
    }
}
Also used : ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) ValidationResult(org.apache.pdfbox.preflight.ValidationResult) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser) PreflightDocument(org.apache.pdfbox.preflight.PreflightDocument) ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) Test(org.junit.Test)

Example 3 with PreflightParser

use of org.apache.pdfbox.preflight.parser.PreflightParser in project pdfbox by apache.

the class TestPDFBox3743 method testPDFBox3743.

/**
 * Test whether use of default colorspace without output intent for text output is detected.
 *
 * @throws IOException
 */
@Test
public void testPDFBox3743() throws IOException {
    DataSource ds = new FileDataSource("src/test/resources/PDFBOX-3743.pdf");
    PreflightParser parser = new PreflightParser(ds);
    parser.parse();
    PreflightDocument document = parser.getPreflightDocument();
    document.validate();
    ValidationResult result = document.getResult();
    document.close();
    // Error should be:
    // 2.4.3: Invalid Color space, /DeviceGray default for operator "Tj" can't be used without Color Profile
    Assert.assertFalse("File PDFBOX-3743.pdf should be detected as not PDF/A-1b", result.isValid());
    Assert.assertEquals("List should contain one result", 1, result.getErrorsList().size());
    Assert.assertEquals("2.4.3", result.getErrorsList().get(0).getErrorCode());
}
Also used : FileDataSource(javax.activation.FileDataSource) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource) Test(org.junit.Test)

Example 4 with PreflightParser

use of org.apache.pdfbox.preflight.parser.PreflightParser in project pdfbox by apache.

the class CreatePDFATest method testCreatePDFA.

/**
 * Test of doIt method of class CreatePDFA.
 */
public void testCreatePDFA() throws Exception {
    System.out.println("testCreatePDFA");
    String pdfaFilename = outDir + "/PDFA.pdf";
    String message = "The quick brown fox jumps over the lazy dog äöüÄÖÜß @°^²³ {[]}";
    String dir = "../pdfbox/src/main/resources/org/apache/pdfbox/resources/ttf/";
    String fontfile = dir + "LiberationSans-Regular.ttf";
    CreatePDFA.main(new String[] { pdfaFilename, message, fontfile });
    PreflightParser preflightParser = new PreflightParser(new File(pdfaFilename));
    preflightParser.parse();
    try (PreflightDocument preflightDocument = preflightParser.getPreflightDocument()) {
        preflightDocument.validate();
        ValidationResult result = preflightDocument.getResult();
        for (ValidationError ve : result.getErrorsList()) {
            System.err.println(ve.getErrorCode() + ": " + ve.getDetails());
        }
        assertTrue("PDF file created with CreatePDFA is not valid PDF/A-1b", result.isValid());
    }
    // check the XMP metadata
    PDDocument document = PDDocument.load(new File(pdfaFilename));
    PDDocumentCatalog catalog = document.getDocumentCatalog();
    PDMetadata meta = catalog.getMetadata();
    DomXmpParser xmpParser = new DomXmpParser();
    XMPMetadata metadata = xmpParser.parse(meta.createInputStream());
    DublinCoreSchema dc = metadata.getDublinCoreSchema();
    assertEquals(pdfaFilename, dc.getTitle());
    document.close();
}
Also used : XMPMetadata(org.apache.xmpbox.XMPMetadata) DomXmpParser(org.apache.xmpbox.xml.DomXmpParser) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) ValidationResult(org.apache.pdfbox.preflight.ValidationResult) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser) File(java.io.File) PreflightDocument(org.apache.pdfbox.preflight.PreflightDocument) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog)

Example 5 with PreflightParser

use of org.apache.pdfbox.preflight.parser.PreflightParser in project pdfbox by apache.

the class Validator_A1b method runSimple.

private static int runSimple(File file) throws IOException {
    ValidationResult result;
    PreflightParser parser = new PreflightParser(file);
    try {
        parser.parse();
        PreflightDocument document = parser.getPreflightDocument();
        document.validate();
        result = document.getResult();
        document.close();
    } catch (SyntaxValidationException e) {
        result = e.getResult();
    }
    if (result.isValid()) {
        System.out.println("The file " + file.getName() + " is a valid PDF/A-1b file");
        System.out.println();
        return 0;
    } else {
        System.out.println("The file " + file.getName() + " is not a valid PDF/A-1b file, error(s) :");
        for (ValidationError error : result.getErrorsList()) {
            System.out.print(error.getErrorCode() + " : " + error.getDetails());
            if (error.getPageNumber() != null) {
                System.out.println(" on page " + (error.getPageNumber() + 1));
            } else {
                System.out.println();
            }
        }
        System.out.println();
        return -1;
    }
}
Also used : SyntaxValidationException(org.apache.pdfbox.preflight.exception.SyntaxValidationException) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser)

Aggregations

PreflightParser (org.apache.pdfbox.preflight.parser.PreflightParser)10 Test (org.junit.Test)6 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)5 SyntaxValidationException (org.apache.pdfbox.preflight.exception.SyntaxValidationException)5 PreflightDocument (org.apache.pdfbox.preflight.PreflightDocument)4 ValidationResult (org.apache.pdfbox.preflight.ValidationResult)4 File (java.io.File)2 ValidationException (org.apache.pdfbox.preflight.exception.ValidationException)2 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 DataSource (javax.activation.DataSource)1 FileDataSource (javax.activation.FileDataSource)1 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)1 PDDocumentCatalog (org.apache.pdfbox.pdmodel.PDDocumentCatalog)1 PDMetadata (org.apache.pdfbox.pdmodel.common.PDMetadata)1 XMPMetadata (org.apache.xmpbox.XMPMetadata)1