|
31 | 31 | //import org.verapdf.processor.reports.multithread.writer.JsonReportWriter; |
32 | 32 | import org.verapdf.pdfa.PDFAValidator; |
33 | 33 | //import org.verapdf.pdfa.flavours.PDFAFlavour; |
| 34 | +import org.verapdf.pdfa.flavours.PDFAFlavour; |
34 | 35 |
|
35 | 36 | public class handler implements RequestHandler<String, String> { |
36 | 37 | @Override |
@@ -69,59 +70,65 @@ public String handleRequest(String input, Context context) { |
69 | 70 |
|
70 | 71 | VeraGreenfieldFoundryProvider.initialise(); |
71 | 72 | String output = ""; |
72 | | - try (PDFAParser parser = Foundries.defaultInstance().createParser(new FileInputStream(filePath))) { |
73 | | - PDFAValidator validator = Foundries.defaultInstance().createValidator(parser.getFlavour(), false); |
| 73 | + PDFAFlavour flavour = PDFAFlavour.PDFUA_2; |
| 74 | + try (PDFAParser parser = Foundries.defaultInstance().createParser(new FileInputStream("mydoc.pdf"), flavour)) { |
| 75 | + PDFAValidator validator = Foundries.defaultInstance().createValidator(flavour, false); |
74 | 76 | ValidationResult result = validator.validate(parser); |
75 | | - |
| 77 | + |
76 | 78 | // Use Jackson ObjectMapper to write the ValidationResult object as JSON |
77 | 79 | ObjectMapper mapper = new ObjectMapper(); |
78 | 80 | output = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getFailedChecks()); |
79 | 81 |
|
80 | | - /* if (result.isCompliant()) { |
81 | | - // File is a valid PDF/A 1b |
82 | | - } else { |
83 | | - // it isn't |
84 | | - } */ |
| 82 | + /* |
| 83 | + * if (result.isCompliant()) { |
| 84 | + * // File is a valid PDF/A 1b |
| 85 | + * } else { |
| 86 | + * // it isn't |
| 87 | + * } |
| 88 | + */ |
85 | 89 | } catch (IOException | ValidationException | ModelParsingException | EncryptedPdfException exception) { |
86 | 90 | // Exception during validation |
87 | 91 | logger.log(exception.toString()); |
88 | 92 | } |
89 | | - // if the file doesn't end in PDF, pass the --nonpdfext flag |
90 | | - /* String nonpdfextFlag = filePath.toString().endsWith(".pdf") |
91 | | - ? "" |
92 | | - : "--nonpdfext "; |
93 | | - ProcessBuilder pb = new ProcessBuilder( |
94 | | - "/opt/java/lib/vera/verapdf", |
95 | | - "-f", |
96 | | - "ua2", |
97 | | - "--format", |
98 | | - "json", |
99 | | - nonpdfextFlag, |
100 | | - filePath.toString()); |
101 | | - pb.redirectErrorStream(true); // also get errors |
102 | 93 |
|
103 | | - String output = ""; |
104 | | - try { |
105 | | - Process process = pb.start(); |
106 | | - StringBuilder outputSb = new StringBuilder(); |
107 | | - try (BufferedReader reader = new BufferedReader( |
108 | | - new InputStreamReader(process.getInputStream()))) { |
109 | | - String line; |
110 | | - // 3. Read line by line until the stream ends |
111 | | - while ((line = reader.readLine()) != null) { |
112 | | - outputSb.append(line).append(System.lineSeparator()); |
113 | | - } |
114 | | - } |
115 | | - int exitCode = process.waitFor(); |
116 | | -
|
117 | | - if (exitCode != 0) { |
118 | | - logger.log("Command exited with non-zero code: " + exitCode); |
119 | | - } |
120 | | - output = outputSb.toString(); |
121 | | -
|
122 | | - } catch (IOException | InterruptedException e) { |
123 | | - logger.log(e.toString()); |
124 | | - } */ |
| 94 | + // if the file doesn't end in PDF, pass the --nonpdfext flag |
| 95 | + /* |
| 96 | + * String nonpdfextFlag = filePath.toString().endsWith(".pdf") |
| 97 | + * ? "" |
| 98 | + * : "--nonpdfext "; |
| 99 | + * ProcessBuilder pb = new ProcessBuilder( |
| 100 | + * "/opt/java/lib/vera/verapdf", |
| 101 | + * "-f", |
| 102 | + * "ua2", |
| 103 | + * "--format", |
| 104 | + * "json", |
| 105 | + * nonpdfextFlag, |
| 106 | + * filePath.toString()); |
| 107 | + * pb.redirectErrorStream(true); // also get errors |
| 108 | + * |
| 109 | + * String output = ""; |
| 110 | + * try { |
| 111 | + * Process process = pb.start(); |
| 112 | + * StringBuilder outputSb = new StringBuilder(); |
| 113 | + * try (BufferedReader reader = new BufferedReader( |
| 114 | + * new InputStreamReader(process.getInputStream()))) { |
| 115 | + * String line; |
| 116 | + * // 3. Read line by line until the stream ends |
| 117 | + * while ((line = reader.readLine()) != null) { |
| 118 | + * outputSb.append(line).append(System.lineSeparator()); |
| 119 | + * } |
| 120 | + * } |
| 121 | + * int exitCode = process.waitFor(); |
| 122 | + * |
| 123 | + * if (exitCode != 0) { |
| 124 | + * logger.log("Command exited with non-zero code: " + exitCode); |
| 125 | + * } |
| 126 | + * output = outputSb.toString(); |
| 127 | + * |
| 128 | + * } catch (IOException | InterruptedException e) { |
| 129 | + * logger.log(e.toString()); |
| 130 | + * } |
| 131 | + */ |
125 | 132 |
|
126 | 133 | logger.log("Processing complete for " + input); |
127 | 134 | logger.log(output); |
|
0 commit comments