question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fill Form - Either missing template or form fields undefined

See original GitHub issue

What were you trying to do?

Fill my template form

Why were you trying to do this?

So I can collect my salary twice a month 😃

How did you attempt to do it?

I saw PDFescape in a post and gave that a shot to be able to edit PDF 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 (dod_character.pdf) 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 -

image

What actually happened?

Details 1 section up

What did you expect to happen?

For my template file to come through with fields filled

How can we reproduce the issue?

My code (nearly copied from yours) -

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)
});

Version

1.17.0

What environment are you running pdf-lib in?

Node

Required Reading

Additional Notes

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dcslinecommented, Nov 12, 2021

@jbw09 I have looked into the screenshot and have seen - you are used another pdf loading process. With your three code lines i could reproduce your error 1076_reproduce_error s.

1reaction
dcslinecommented, Nov 8, 2021

@jbw09: I have checked your test code - without problems! A Question, why do you not using Hopdings program posibility to read the field names. I had built-in this code

const fields = pdfDoc.getForm().getFields() const fieldNames = fields.map((f) => f.getName()); fields.forEach(field => { const name = field.getName() console.log(‘Field name:’, name); const widgets = field.acroField.getWidgets(); })

in the sample and could see the following field names: Field name: prodCode Field name: certNumber Field name: model Field name: serial Field name: date

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node JS pdf-lib - Fill Form - Either missing template or form ...
I tried the Fill Form example file and it couldn't find the form field names, so I also loaded that in PDFescape and...
Read more >
Fields are missing in the fillable PDF form - Jotform
You have some conditions with deleted fields (Undefined Fields), this is causing the issue in the fillable PDF. Deleting these should help ...
Read more >
JavaScript : HTML form - Checking for non empty - w3resource
You can write a JavaScript form validation script to check whether the required field(s) in the HTML form is blank or not.
Read more >
Working with Angular 4 Forms: Nesting and Input Validation
Angular 4 makes it especially easy for both template-driven and reactive forms. Learn the different approaches in Angular 4 form validation.
Read more >
undefined - JavaScript - MDN Web Docs - Mozilla
The global undefined property represents the primitive value ... and inequality operators to determine whether a variable has a value.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found