Some test text!

menu

Convert PDF to PDF/A in JavaScript

More languages

chevron_right
More languages
JavaScript
Java (Android)
C++
C#
C# (.NET Core)
Java
Kotlin
Obj-C
JS (Node.js)
PHP
Python
Ruby
Swift
C# (UWP)
VB
C# (Xamarin)

Sample JavaScript code for using PDFTron 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 JavaScript PDF Library and PDF/A Library. A command-line tool for batch conversion and validation is also available.

Get StartedSamplesDownload

To run this sample, get started with a free trial of PDFTron SDK.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

const { PDFNet } = require('../../../lib/pdfnet.js');

((exports) => {

  exports.runPDFA = () => {

    const main = async() => {
      try {
        console.log('PDFA validation test begins.');

        const inputURL = '../../TestFiles/';
        const inputFilename = 'newsletter.pdf';

        const convert = false;
        const pwd = '';
        let exceptions;
        const maxRefObjs = 10;
        const firstStop = false;
        const url = inputURL + inputFilename;

        const pdfa = await PDFNet.PDFACompliance.createFromFile(convert, url, pwd, PDFNet.PDFACompliance.Conformance.e_Level1B, exceptions, maxRefObjs, firstStop);

        const errorCount = await pdfa.getErrorCount();
        if (errorCount === 0) {
          console.log(inputFilename + ' is a valid PDFA.');
        } else {
          console.log(inputFilename + ' is NOT a valid PDFA.');
          for (let i = 0; i < errorCount; i++) {
            const errorCode = await pdfa.getError(i);
            const errorMsg = await PDFNet.PDFACompliance.getPDFAErrorMessage(errorCode);
            const numRefs = await pdfa.getRefObjCount(errorCode);
            if (numRefs > 0) {
              const objs = [];
              for (let j = 0; j < numRefs; j++) {
                const objRef = await pdfa.getRefObj(errorCode, j);
                objs.push(objRef);
              }
              console.log('Error:' + errorMsg + '. Objects:' + objs.toString());
            }
          }
        }
      } catch (err) {
        console.log(err);
      }
      try {
        console.log('PDFA conversion test begins.');

        const inputURL = '../../TestFiles/';
        const inputFilename = 'fish.pdf';
        const outputFilename = 'fish_pdfa.pdf';

        const convert = true;
        const pwd = '';
        let exceptions;
        const maxRefObjs = 10;
        const urlInput = inputURL + inputFilename;

        console.log('Converting input document: ' + inputFilename);
        const pdfa = await PDFNet.PDFACompliance.createFromFile(convert, urlInput, pwd, PDFNet.PDFACompliance.Conformance.e_Level1B, exceptions, maxRefObjs);

        const errorCount = await pdfa.getErrorCount();
        if (errorCount === 0) {
          console.log(inputFilename + ' is a valid PDFA.');
        } else {
          console.log(inputFilename + ' is NOT a valid PDFA.');
        }

        console.log('Save and validate the converted document: ' + outputFilename);
        const linearize = true;
        await pdfa.saveAsFromFileName('../../TestFiles/Output/' + outputFilename, linearize);
        const validateOnly = false;
        const docBuffer = await pdfa.saveAsFromBuffer(linearize);
        const pdfaValidate = await PDFNet.PDFACompliance.createFromBuffer(validateOnly, docBuffer, pwd, PDFNet.PDFACompliance.Conformance.e_Level1B, exceptions, maxRefObjs);
        const errorCountValidate = await pdfaValidate.getErrorCount();
        if (errorCountValidate === 0) {
          console.log(outputFilename + ' is a valid PDFA.');
        } else {
          console.log(outputFilename + ' is NOT a valid PDFA.');
        }
      } catch (err) {
        console.log(err);
      }
    };

    // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
    PDFNet.runWithCleanup(main).then(function(){PDFNet.shutdown();});
  };
  exports.runPDFA();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=PDFATest.js
close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

Join our upcoming webinar to learn about how to collaborate on videos frame by frame directly in your browser

Save your seat
close