pdf-lib: Fill Form - Either missing template or form fields undefined
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 -
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
- I have read www.sscce.org.
- My report includes a Short, Self Contained, Correct (Compilable) Example.
- I have read Smart Questions.
- I have read 45 GitHub Issues Dos and Donāts.
Additional Notes
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (3 by maintainers)
@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
@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
s.