Some test text!

Search
Hamburger Icon

Convert PDF to PDF/A in JavaScript

More languages

More languages
JavaScript
Java (Android)
C++
C#
C# (.NET Core)
Go
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 Started Samples Download

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

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

const { PDFNet } = require('@pdftron/pdfnet-node');
const PDFTronLicense = require('../LicenseKey/LicenseKey');

((exports) => {

  exports.runPDFA = () => {

    const printResults = async (pdfa, filename) => {

      const errorCount = await pdfa.getErrorCount();
      if (errorCount === 0) {
        console.log(filename + ': OK.');
      } else {
        console.log(filename + ' 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);
          console.log(' - e_PDFA ' + errorCode + ': ' + errorMsg + '.');
          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('   Objects: ' + objs.join(', '));
          }
        }
        console.log('');
      }
    }

    //---------------------------------------------------------------------------------------
    // The following sample illustrates how to parse and check if a PDF document meets the
    //	PDFA standard, using the PDFACompliance class object. 
    //---------------------------------------------------------------------------------------
    const main = async () => {
      const inputPath = '../TestFiles/';
      const outputPath = inputPath + 'Output/';
      await PDFNet.setColorManagement();  // Enable color management (required for PDFA validation).

      //-----------------------------------------------------------
      // Example 1: PDF/A Validation
      //-----------------------------------------------------------
      try {
        const filename = 'newsletter.pdf';
        /* The max_ref_objs parameter to the PDFACompliance constructor controls the maximum number 
        of object numbers that are collected for particular error codes. The default value is 10 
        in order to prevent spam. If you need all the object numbers, pass 0 for max_ref_objs. */
        const pdfa = await PDFNet.PDFACompliance.createFromFile(false, inputPath + filename, '', PDFNet.PDFACompliance.Conformance.e_Level2B);
        await printResults(pdfa, filename);
      } catch (err) {
        console.log(err);
      }

      //-----------------------------------------------------------
      // Example 2: PDF/A Conversion
      //-----------------------------------------------------------
      try {
        let filename = 'fish.pdf';
        const pdfa = await PDFNet.PDFACompliance.createFromFile(true, inputPath + filename, '', PDFNet.PDFACompliance.Conformance.e_Level2B);
        filename = 'pdfa.pdf';
        await pdfa.saveAsFromFileName(outputPath + filename);

        // Re-validate the document after the conversion...
        const comp = await PDFNet.PDFACompliance.createFromFile(false, outputPath + filename, '', PDFNet.PDFACompliance.Conformance.e_Level2B);
        await printResults(comp, filename);
      } catch (err) {
        console.log(err);
      }

      console.log('PDFACompliance test completed.')
    };

    PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) { console.log('Error: ' + JSON.stringify(error)); }).then(function () { return PDFNet.shutdown(); });
  };
  exports.runPDFA();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=PDFATest.js