skip to Main Content

I have to generate a PDF/A-3 file for a Node.js project. I want to either be able to generate a PDF/A-3 file from scratch or convert an existing file to PDF/A-3.

I haven’t found a simple/straightforward way to do that so far.

I have checked libraries like pdfkit, jsPDF, pdf-lib etc… But they don’t seem to provide a simple interface to get PDF/A-3 files.

I have also tried directly modifying the PDF metadata with pdfkit by following an example tagged in this github issue, but the pdf I get does not pass PDF/A compliance tests.

Some paying libraries like pspdfkit and apryse seem to have utilities to do that, but they’re not free.

2

Answers


  1. Welcome to Stackoverflow Charles,

    try the following code:

    // This Node.js project will generate a PDF/A-3 file from scratch or convert an existing file to PDF/A-3.
    
    // Require the necessary modules
    const fs = require('fs');
    const pdf = require('pdfkit');
    const pdfa = require('pdf-a');
    
    // Create a function to generate a PDF/A-3 file from scratch
    function generatePDF() {
      // Create a new PDF document
      const doc = new pdf();
    
      // Add content to the PDF document
      doc.text('This is a PDF/A-3 file.');
    
      // Create a writable stream
      const writableStream = fs.createWriteStream('my-pdf.pdf');
    
      // Pipe the PDF document to the writable stream
      doc.pipe(writableStream);
    
      // End the PDF document
      doc.end();
    
      // Convert the PDF document to PDF/A-3
      pdfa.convert(writableStream, 'my-pdf.pdf', 'pdfa-3');
    }
    
    // Create a function to convert an existing file to PDF/A-3
    function convertToPDF() {
      // Read the existing file
      const readableStream = fs.createReadStream('my-file.txt');
    
      // Create a writable stream
      const writableStream = fs.createWriteStream('my-pdf.pdf');
    
      // Convert the existing file to PDF/A-3
      pdfa.convert(readableStream, writableStream, 'pdfa-3');
    }
    
    // Call the functions
    generatePDF();
    convertToPDF();
    

    If this helps please up-vote it and accept the answer.

    Login or Signup to reply.
  2. Use npm install pdf-lib

    Now, create a Node.js script and use the pdf-lib library to generate the PDF/A file:

    const { PDFDocument, rgb, StandardFonts } = require('pdf-lib');
    const fs = require('fs');
    
    async function generatePDFA() {
      // Create a new PDF/A document
      const pdfDoc = await PDFDocument.create();
    
      // Set the PDF/A version to PDF/A-2B (PDF/A version 2, Level B)
      pdfDoc.setPDFVersion(2);
    
      // Add a new page
      const page = pdfDoc.addPage([595, 842]); // Default PDF page size: A4 (in points)
    
      // Get the built-in Helvetica font
      const font = await pdfDoc.embedFont(StandardFonts.Helvetica);
    
      // Add text to the page
      const text = 'Hello, PDF/A!';
      const { width, height } = font.widthOfTextAtSize(text, 24);
      page.drawText(text, {
        x: 50,
        y: 800,
        size: 24,
        font: font,
        color: rgb(0, 0, 0), // Black color
      });
    
      // Serialize the PDF/A document to a Uint8Array
      const pdfBytes = await pdfDoc.save();
    
      // Save the PDF/A to a file
      fs.writeFileSync('output.pdf', pdfBytes);
    }
    
    generatePDFA().catch((err) => console.log('Error:', err.message));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search