-
-
Notifications
You must be signed in to change notification settings - Fork 698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
acro form fields blank in either downloaded or streamed PDF #488
Comments
Hey, I am not sure if this will help. But I had a similar issue this morning where after I ran So instead of using acroField.set(PDFName.of('V'), PDFString.of(text)); I replaced it with acroField.set(PDFName.of('V'), PDFHexString.fromText(text.toString())); May have been my text not really being text so I added |
thanks you for your suggestion @lalaman will try it and let you know. In the meantime I have some more debug info. We're using pdfjs by mozilla to render the PDF in the frontend. This is throwing some warnings, an excerpt:
We are trying to fill one field with "Managing Director" and something doesn't seem to work properly when adding the acroform field values, it kinda chops all of that into pieces. I'm not that familiar with all this PDF stuff... maybe @Hopding can use this info. |
Okay, I found out even more info about this. I changed everything back to the implementation recommended in #205 (comment) because I had a hunch that it might not be the PDF itself but the different apps/previews which I used to view it. Turns out I wasn't completely wrong. Here is what is (not) working now:
I have not clue how and why this happens, I just felt like documenting this in it entierty would be helpful at some point. We decided to go with the "industry standard" Adobe Reader. So for now we're good. I'd be happy to help with any further investigation on this topic. |
Same thing is happening to me but only if I copy the filled pages to a new document and then save the second one. |
Unfortunately, this issue has been brought up many times in this repository as copyPages does not copy over acrofields from the original pages. What I ended up doing was using a combination of pdf-lib to remove pages (since that maintains the acrofields) and then using pdf-merge to join the documents together. This way, the pdfs are merged and retain the acroform fields. After you join, you still have to use PDFDocument.load() to set needAppearances to true in order to see the text inside the acroform fields. Here is an example: const fs = require('fs');
const { PDFBool, PDFDocument, PDFName } = require('pdf-lib');
const PDFMerge = require('pdf-merge');
// Helper function for removing pages in PDF
const removePDFPages = async (doc, start, end) => {
for (let i = end; i >= start; i -= 1) {
doc.removePage(i);
}
const docBytes = await doc.save();
const filepath = `${Date.now()}.pdf`;
fs.writeFileSync(filepath, docBytes);
return filepath;
};
// Join pages 1-10 of doc1 and 1-5 of doc2
const combinePDFs = async (doc1filepath, doc2filepath) => {
let tempFilepaths = [];
try {
const doc1 = await PDFDocument.load(fs.readFileSync(doc1filepath));
const doc1pages = doc1.getPages();
const doc2 = await PDFDocument.load(fs.readFileSync(doc2filepath));
const doc2pages = doc2.getPages();
const temp1filepath = await removePDFPages(doc1, 10, doc1pages - 1);
tempFilepaths.push(temp1filepath);
const temp2filepath = await removePDFPages(doc2, 5, doc2pages - 1);
tempFilepaths.push(temp2filepath);
const mergedBuffer = await PDFMerge([
temp1filepath,
temp2filepath
]);
// Set appearances to true so that your form fields will not
// appear as white text
const mergedDoc = await PDFDocument.load(mergedBuffer);
const acroForm = mergedDoc.context.lookup(
mergedDoc.catalog.get(PDFName.of('AcroForm')),
);
acroForm.set(PDFName.of('NeedAppearances'), PDFBool.True);
const mergedDocBytes = await mergedDoc.save();
const destination = `${Date.now()}-final.pdf`;
fs.writeFileSync(destination, mergedDocBytes);
return destination;
} catch (err) {
console.log('Unable to combine PDFs', err);
throw new Error(err);
} finally {
// Delete temp files
for (const path of tempFilepaths) {
if (fs.existsSync(path)) {
fs.unlinkSync(path);
}
}
}
}; |
Hello @faxemaxee @lalaman @freirg! pdf-lib has a new forms API that should solve this problem. See the README and API docs for details. (Note that the new forms API does not allow forms to be copied from other documents. That's still an issue. See #218 and related issues for details.) |
Hey, we are using your great lib to fill form field in a pdf and sending it back to the user in two ways. First we are directly streaming the PDF to update a PDF Preview on the client and after finished the edit we persist the PDF and just deliver the PDF via GET Url.
We built a small helper to generate the filled PDF and return the bytes after we performed
pdfDoc.save()
. We use the exact same method in both cases but depending on wether we stream it directly or save the file we get either blank fields or filled fields. We had an old implementation – found somewhere in this repo – which only showed the filled fields when we persisted the PDF but not in preview mode. We then updated this implementation as stated in the Update to V1.X.X guide in the readme, this made the preview work but broke the persisted PDF. After that we started from scratch with the solution provided in #205 which seemed promising but again, only the preview worked, not the persisted PDF.So we are kinda out of ideas here, maybe you have any further suggestions?
pdfhelper.js (only working as persisted PDF)
helper.js (only working as persisted PDF)
routes.js (excerpt)
The text was updated successfully, but these errors were encountered: