question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Error: Expected instance of PDFDict or PDFStream, but got instance of undefined" on save()

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
aman-sarafcommented, Nov 10, 2020

@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-lib expects the PDF in some standards, even though the load method 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 updateFieldAppearances on save for 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 updateFieldAppearances doesn’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 PDF to PDF/A seems to be the only solution for the above problem.

1reaction
aman-sarafcommented, Nov 10, 2020

@Wian-TMC . Glad to know that it worked for you. Yeah, you’re correct in pointing the flaw in updateFieldAppearances. The conversion of PDF standard to PDF/A standard seems to be the best solution.

Read more comments on GitHub >

github_iconTop 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 >
pdf-lib - UNPKG
The CDN for pdf-lib.
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found