Sample JavaScript code for using Apryse SDK to programmatically convert generic PDF documents into ISO-compliant, VeraPDF-valid PDF/A files, or to validate PDF/A compliance. Supports all three PDF/A parts (PDF/A-1, PDF/A-2, PDF/A-3), and covers all conformance levels (A, B, U). Learn more about our Web SDK and PDF/A Library. A command-line tool for batch conversion and validation is also available.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5(exports => {
6
7
8
9
10 exports.runPDFA = () => {
11 const PDFNet = exports.Core.PDFNet;
12
13 const main = async () => {
14 try {
15 console.log('PDFA validation test begins.');
16
17 const inputURL = '../TestFiles/';
18 const inputFilename = 'newsletter.pdf';
19
20 const convert = false;
21 const pwd = '';
22 let exceptions;
23 const maxRefObjs = 10;
24 const firstStop = false;
25 const url = inputURL + inputFilename;
26
27 const pdfa = await PDFNet.PDFACompliance.createFromUrl(convert, url, pwd, PDFNet.PDFACompliance.Conformance.e_Level1B, exceptions, maxRefObjs, firstStop);
28
29 const errorCount = await pdfa.getErrorCount();
30 if (errorCount === 0) {
31 console.log(inputFilename + ' is a valid PDFA.');
32 } else {
33 console.log(inputFilename + ' is NOT a valid PDFA.');
34 for (let i = 0; i < errorCount; i++) {
35 const errorCode = await pdfa.getError(i);
36 const errorMsg = await PDFNet.PDFACompliance.getPDFAErrorMessage(errorCode);
37 const numRefs = await pdfa.getRefObjCount(errorCode);
38 if (numRefs > 0) {
39 const objs = [];
40 for (let j = 0; j < numRefs; j++) {
41 const objRef = await pdfa.getRefObj(errorCode, j);
42 objs.push(objRef);
43 }
44 console.log('Error:' + errorMsg + '. Objects:' + objs.toString());
45 }
46 }
47 }
48 } catch (err) {
49 console.log(err);
50 }
51 try {
52 console.log('PDFA conversion test begins.');
53
54 const inputURL = '../TestFiles/';
55 const inputFilename = 'fish.pdf';
56 const outputFilename = 'fish_pdfa.pdf';
57
58 const convert = true;
59 const pwd = '';
60 let exceptions;
61 const maxRefObjs = 10;
62 const urlInput = inputURL + inputFilename;
63
64 console.log('Converting input document: ' + inputFilename);
65 const pdfa = await PDFNet.PDFACompliance.createFromUrl(convert, urlInput, pwd, PDFNet.PDFACompliance.Conformance.e_Level1B, exceptions, maxRefObjs);
66
67 const errorCount = await pdfa.getErrorCount();
68 if (errorCount === 0) {
69 console.log(inputFilename + ' is a valid PDFA.');
70 } else {
71 console.log(inputFilename + ' is NOT a valid PDFA.');
72 }
73
74 console.log('Save and validate the converted document: ' + outputFilename);
75 const linearize = true;
76 const docBuffer = await pdfa.saveAsFromBuffer(linearize);
77 saveBufferAsPDFDoc(docBuffer, outputFilename);
78 const validateOnly = false;
79 const pdfaValidate = await PDFNet.PDFACompliance.createFromBuffer(validateOnly, docBuffer, pwd, PDFNet.PDFACompliance.Conformance.e_Level1B, exceptions, maxRefObjs);
80 const errorCountValidate = await pdfaValidate.getErrorCount();
81 if (errorCountValidate === 0) {
82 console.log(outputFilename + ' is a valid PDFA.');
83 } else {
84 console.log(outputFilename + ' is NOT a valid PDFA.');
85 }
86 } catch (err) {
87 console.log(err);
88 }
89 };
90
91 // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
92 PDFNet.runWithCleanup(main);
93 };
94})(window);
95// eslint-disable-next-line spaced-comment
96//# sourceURL=PDFATest.js
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales