How to join different PDFs with AcroForms?
Hello, I am trying to join different acroforms on a page, but when I try to join them, the fields are blank, I am saving them with the save () method, I also use embedpdf and that is when the values no longer appear, then I used PDFMake to join, but neither works.
export const modifyHeaderDoc = async (data: any) => {
const headerPDF = '/assets/header.pdf'
const fieldNames = {
.....
};
const existingPdfBytes = await fetch(headerPDF).then((res) => {
console.log("res", res);
return res.arrayBuffer();
});
const pdfDoc = await PDFDocument.load(existingPdfBytes);
const acroForm = getAcroForm(pdfDoc);
acroForm.set(PDFName.of("NeedAppearances"), PDFBool.True);
fillInField(pdfDoc, fieldNames.form, data. ...);
.
.
.
// Now make all the fields read-only
const acroFields = await getAcroFields(pdfDoc);
await acroFields.forEach((field) => {
lockField(field);
});
const pdfBytes = pdfDoc.save();
savepdf(pdfBytes64, "E14");
///// If I save here, works great, but i need to join with outher two or more acroforms
return pdfBytes;
};
Then I need use pdf make to use like image with some other existin format in this library, but my fields gone
const generaE14 = async (data: any) => {
const header = await modifyHeader(data);
const image = await convertToImage(header);
const documentDefinition = {
pageSize: { width: 226, height: "auto" },
pageMargins: [10, 20, 10, 20],
content: [
{
image,
width: 226,
height: 300,
},
],
};
await pdfMake.createPdf(documentDefinition).download();
};
Or if i trying to use pdf lib doesn't work too
export async function createPdf(modifyHeader) {
const header = await PDFDocument.load(modifyHeader);
const pdfDoc = await PDFDocument.create();
const head = await pdfDoc.embedPdf(header);
const page = pdfDoc.addPage();
page.drawPage(head[0]);
const pdfBytes = await pdfDoc.saveAsBase64();
savepdf(pdfBytes, "E14"); //Here i save buy it's in blank
}
Now, I'm going to attach the result of this 3 functions
The PDF I Need to add a new document E14 (76).pdf
The result with PDFMake file - 2020-04-21T084516.640.pdf
The result with pdf-lib create & pdf-lib embed page or embed pdf (the result its the same) E14 (50).pdf
Hello @davidra24!
The issue you're encountering has the same root cause as https://github.com/Hopding/pdf-lib/issues/218, https://github.com/Hopding/pdf-lib/issues/341, https://github.com/Hopding/pdf-lib/issues/159, and https://github.com/Hopding/pdf-lib/issues/402. Basically, when copying pages between documents, pdf-lib does not copy certain "external" structures. This includes acroforms.
I'm afraid there's no straightforward solution to this at the moment. But I do plan to support copying external structures (hopefully automatically) in the future. If anybody is interested in working on a PR for this, I'd be happy to provide some pointers.
Closed in favor of other issues that track the same root cause. I'll probably create a new parent issue to track this in the future.
late to the party. if it is still helpful, I had the same issue, and was able to solve it thanks to this pr: https://github.com/Hopding/pdf-lib/pull/599/files
basically get all fields from the pdf we are merging (pdfDocI) and add it to the acroForm of the output pdf (pdfDocII):
pdfDocI.getForm().getFields().forEach(field => {
const newRef = pdfDocII.context.register(field.acroField.dict)
pdfDocII.getForm().acroForm.addField(newRef)
})
When document contain digital signatures, my opinion is that those should not be copied into new document because it is extremely difficult to keep signature valid. If document has docMDP permissions set it makes copying even more difficult. Invalid signatures are not looking good in merged document. Thereof it would be better not to copy those. (Also, digital signatures are meant to show user that this document is intact, if signer has not been able to add MDP permissions properly this kind of merging could provide valid signature into document that has extra content.)
Also copying other fields is risky; field names can collide, pages may not be copied in same order so references may get broken etc. So this could be done reliably only by the 3rd party who knows what needs to be done, not lib-pdf.