"Error: Expected instance of PDFDict or PDFStream, but got instance of undefined" on save()
See original GitHub issueI have a funky (possibly corrupt) PDF that fails to fill. When I run save(), I hit the following error:
Error: Expected instance of PDFDict or PDFStream, but got instance of undefined
at new UnexpectedObjectTypeError (/path/to/myproject/node_modules/pdf-lib/cjs/core/errors.js:40:24)
at PDFContext.lookup (/path/to/myproject/node_modules/pdf-lib/cjs/core/PDFContext.js:78:15)
at PDFDict.lookup (/path/to/myproject/node_modules/pdf-lib/cjs/core/objects/PDFDict.js:51:43)
at PDFWidgetAnnotation.PDFAnnotation.getAppearances (/path/to/myproject/node_modules/pdf-lib/cjs/core/annotation/PDFAnnotation.js:75:20)
at PDFTextField.needsAppearancesUpdate (/path/to/myproject/node_modules/pdf-lib/cjs/api/form/PDFTextField.js:619:48)
at PDFForm.updateFieldAppearances (/path/to/myproject/node_modules/pdf-lib/cjs/api/form/PDFForm.js:460:23)
at PDFDocument.<anonymous> (/path/to/myproject/node_modules/pdf-lib/cjs/api/PDFDocument.js:1150:38)
at step (/path/to/myproject/node_modules/tslib/tslib.js:141:27)
at Object.next (/path/to/myproject/node_modules/tslib/tslib.js:122:57)
at /path/to/myproject/node_modules/tslib/tslib.js:115:75
I was able to fix this locally by adding a try{}catch{} around needApperancesUpdate. Should there be some way to fail gracefully here? Or any idea why the error might be happening?
The PDF: CCC901.pdf
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:7 (1 by maintainers)
Top Results From Across the Web
"Error: Expected instance of PDFDict or PDFStream, but got ...
"Error: Expected instance of PDFDict or PDFStream, but got instance of undefined" on save()
Read more >Solved: Expected a dict object and This document enabled e...
Solved: Suddenly getting these errors when trying to open a PDF we have used before. Oddly, it will open in Edge browser and...
Read more >PdfSignatureField Class | GemBox.Pdf.Forms
To set a non-null value, use any SetLockedFields() method instead. ... The PdfSignatureField value as an instance of the PdfSignature type.
Read more >PDFsharp save to MemoryStream - Stack Overflow
If you think there is an issue with PdfDocument.Save, then please report this on the PDFsharp forum (but please be more specific with...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@schlosser . I don’t think that adding try catch block locally is a good idea. From my investigation, it looks like that the PDF document is corrupt. It seems that the
pdf-libexpects the PDF in some standards, even though theloadmethod won’t throw error for a corrupt pdf file.In order to check whether your pdf file is compliant with standards, you may use online checkers such as https://www.pdfen.com/pdf-a-validator . Most probably, it will notify you of the errors & you may convert the pdf into specific standard & try again .
I hope it helps !
Update
I just tried
updateFieldAppearancesonsavefor a corrupt PDF file and it worked like a charm.You may use
await pdfDoc.save({updateFieldAppearances: false})and see if error still appears.
Update
The
updateFieldAppearancesdoesn’t seem to work for multiple fields or it may also obscure the updated text which will be only visible once you click on the field.Hence, the conversion of standard from
PDFtoPDF/Aseems to be the only solution for the above problem.@Wian-TMC . Glad to know that it worked for you. Yeah, you’re correct in pointing the flaw in
updateFieldAppearances. The conversion ofPDFstandard toPDF/Astandard seems to be the best solution.