You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are trying to sign flattened PDFs using a certificate. The PDFs were flattened using pdf-lib after filling them in. (filling doesn't matter. We can skip filling and we still get the same error.)
How did you attempt to do it?
We just load the PDF (code below) and then we flatten it. That's it.
What actually happened?
Getting the below error suggesting the PDF is corrupt. Do you have any idea why?
What did you expect to happen?
To not get this error.
How can we reproduce the issue?
here is the function that generates the flattened PDF:
functionreport(){constpdfPath=join(__dirname,"wh347.pdf");constpdfBuffer=awaitfs.readFile(pdfPath);constoutputPdfDoc=awaitPDFDocument.create();consttemplatePdfDoc=awaitPDFDocument.load(pdfBuffer);consttemplateForm=templatePdfDoc.getForm();templateForm.flatten();// Copy both pages from the filled templateconst[filledPage1]=awaitoutputPdfDoc.copyPages(templatePdfDoc,[0]);outputPdfDoc.addPage(filledPage1);// Save the PDFconstpdfBytes=awaitoutputPdfDoc.save();return{type: "pdf"asconst,data: Buffer.from(pdfBytes),};
You can use the buffer to save the file on disk.
I attached the unfilled PDF (file pdf-A.pdf) which can be signed using a certificate. I also attached the flattened pdf (file pdf-B.pdf) which was flattened using pdf-lib. This one cannot be signed using a certificate.
My report includes a Short, Self Contained, Correct (Compilable) Example.
I have attached all PDFs, images, and other files needed to run my SSCCE.
Additional Notes
I did a test and I opened the fillable PDF (file pdf-A.pdf) in Chrome, filled it and printed (so it gets flattened). Then, I went to Acrobat to sign it using a certificate as I did before and it works.
It seems there is a problem in how pdf-lib flattens PDFs, since signing works when I flatten using Chrome.
Also, if I don't flatten the PDF, signing works. But opening the PDF in Acrobat doesn't display the values in the fillable fields unless I click on that field, which is weird:
Screen.Recording.2025-08-29.at.23.27.12.mov
But, we need to flatten the PDFs, so not flattening is not an option.
What were you trying to do?
We are trying to sign flattened PDFs using a certificate. The PDFs were flattened using pdf-lib after filling them in. (filling doesn't matter. We can skip filling and we still get the same error.)
How did you attempt to do it?
We just load the PDF (code below) and then we flatten it. That's it.
What actually happened?
Getting the below error suggesting the PDF is corrupt. Do you have any idea why?
What did you expect to happen?
To not get this error.
How can we reproduce the issue?
here is the function that generates the flattened PDF:
You can use the buffer to save the file on disk.
I attached the unfilled PDF (file
pdf-A.pdf) which can be signed using a certificate. I also attached the flattened pdf (filepdf-B.pdf) which was flattened using pdf-lib. This one cannot be signed using a certificate.pdf-A.pdf
pdf-B.pdf
Version
1.17.1
What environment are you running pdf-lib in?
Node
Checklist
Additional Notes
I did a test and I opened the fillable PDF (file
pdf-A.pdf) in Chrome, filled it and printed (so it gets flattened). Then, I went to Acrobat to sign it using a certificate as I did before and it works.It seems there is a problem in how pdf-lib flattens PDFs, since signing works when I flatten using Chrome.
Also, if I don't flatten the PDF, signing works. But opening the PDF in Acrobat doesn't display the values in the fillable fields unless I click on that field, which is weird:
Screen.Recording.2025-08-29.at.23.27.12.mov
But, we need to flatten the PDFs, so not flattening is not an option.