0

I saw PDFescape in another post and gave that a shot to be able to edit form field names. Ran my prog and indeed form fields filled but the rest of the contents from my template file were missing.

I tried the Fill Form example file and it couldn't find the form field names, so I also loaded that in PDFescape and saved from there, and then the same result of the rest of the template missing but form fields filled.

I thought maybe PDFescape could be the issue so I purchased the Adobe trial to be able to edit the form field names but there again, pdf-lib doesn't find them (Error: PDFDocument has no form field with the name "prodCode") even though definitely saved as such -

enter image description here

Where the heck am I going wrong?! Code for your reference -

const { PDFDocument } = require('pdf-lib');
const fs = require('fs');

(async () => {

    const pdfUTF8 = fs.readFileSync('./test.pdf','utf8')
    var formPdfBytes = new TextEncoder("utf-8").encode(pdfUTF8);
  
    // Load a PDF with form fields
    const pdfDoc = await PDFDocument.load(formPdfBytes)

    // Get the form containing all the fields
    const form = pdfDoc.getForm()

    // Get all fields in the PDF by their names
    const productCodeField = form.getTextField('prodCode')
    const certNumberField = form.getTextField('certNumber')

    productCodeField.setText('Product code here')
    certNumberField.setText('Cert number here')

    // Serialize the PDFDocument to bytes (a Uint8Array)
    const pdfBytes = await pdfDoc.save()

    const data = fs.writeFileSync('./done.pdf', new Buffer.from(pdfBytes))

})().catch(e => {
  console.log(e)
});
stm
  • 662
  • 1
  • 6
  • 23
JBW
  • 63
  • 8

1 Answers1

0

I read documentation for over a dozen packages and implemented about 5, the only one I could get to work with the requirements being in Node JS and can load a remote PDF file from my MongoDB and fill it was pdfform.js.

const pdfform = require('pdfform.js');

const Mail = require('./tools/sendgrid/sendgrid');

const mongoose = require('mongoose');

mongoose.connect(process.env.MONGODB_URI, {useNewUrlParser: true, useUnifiedTopology: true})
const db = mongoose.connection
db.on('error', (error) => console.error(error))
db.once('open', () => console.log('Connected to Database'))


db.collection('warrantycerts').findOne({_id : 4}, function(err, doc){
    if (err) {
        console.error(err);
    }
    
    const pdf_buf = doc.bin.buffer

    var pdfFieldsJson = pdfform().list_fields(pdf_buf)

    console.log(pdfFieldsJson)

    var fields = {
        'prodCode': ['prod code here'],
        'certNumber': ['cert here'],
        'model': ['model here'],
        'serial': ['serial here'],
        'date': ['11-10-21'],
    };

    var out_buf = pdfform().transform(pdf_buf, fields);

    Mail.emailAttach(me, 'testing', 'testing', 'cert_test.pdf', new Buffer.from(out_buf).toString('base64'))

})

FYI issue regarding pdf-lib was submitted but not much traction of yet.

JBW
  • 63
  • 8