pdf-lib
pdf-lib copied to clipboard
Unable to edit the same pdf form data in a loop and merge to single pdf file.
What are you working on?
I am trying to make a certificate system when I have a preset pdf with form fields. I have attempted to loop the form data with an array of participants however the end product results in only one participant's name for all the pages. As per the example below - only "Joe" will be shown as the participant for all 3 resultant pages.
Participants = ["Joe","Allen",Bob"];
async function fillForm(participant) {
const formUrl = './Certificate.pdf'
const formPdfBytes = await fetch(formUrl).then(res => res.arrayBuffer())
const pdfDocFile = await PDFDocument.load(formPdfBytes)
const form = pdfDocFile.getForm()
const participantField = form.getTextField('Participant')
participantField.setText(participant)
return pdfDocFile;
}
async function generateForms(){
const mergedPdf = await PDFDocument.create();
for (let x in participants) {
dataURI = await fillForm(participants[x]);
const copyPages = await mergedPdf.copyPages(dataURI, dataURI.getPageIndices());
const process = copyPages.forEach((page) => mergedPdf.addPage(page));
}
return await mergedPdf;
}
Additional Notes
No response
@Freetodream Hi, I tried using your code to reproduce the bug but I got the correct pdfs generated.
is there a chance that copyPages
loses the form data in this case?
I seem to have a similar flow but wanted to copy pages from source document and fill them. Once I run a copyPages
, the copied ones don't seem to have a form anymore.
@ikari-pl I'm running into a similar problem. I don't see a way to combine pdf's without running copyPages
first.