pdf-lib icon indicating copy to clipboard operation
pdf-lib copied to clipboard

Unable to edit the same pdf form data in a loop and merge to single pdf file.

Open Freetodream opened this issue 2 years ago • 1 comments

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 avatar Mar 17 '22 09:03 Freetodream

@Freetodream Hi, I tried using your code to reproduce the bug but I got the correct pdfs generated.

mergePdfs_test.pdf

happytogether avatar Jul 13 '22 08:07 happytogether

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 avatar Oct 07 '22 09:10 ikari-pl

@ikari-pl I'm running into a similar problem. I don't see a way to combine pdf's without running copyPages first.

shadoath avatar Oct 12 '22 10:10 shadoath