pdf-lib icon indicating copy to clipboard operation
pdf-lib copied to clipboard

Expected instance of PDFDict or PDFStream, but got instance of undefined

Open prd-phuc-nim opened this issue 4 years ago • 5 comments

What were you trying to do?

Expected instance of PDFDict or PDFStream, but got instance of undefined

How did you attempt to do it?

I flattened a PDF fillable file and want to get Buffer data from it. But an error occurred: Expected instance of PDFDict or PDFStream, but got instance of undefined my code: note: bufferData is a Buffer data which got from PDF fillable file

const pdfDoc = await PDFDocument.load(bufferData);
cosnt form = pdfDoc.getForm();
form.flatten();
const resultBytes = await pdfDoc.save();

But when i use a PDF fillable file which has one page, that works fine! My files: 0000013_ArizonaForm.pdf

What actually happened?

at new UnexpectedObjectTypeError (/app/node_modules/pdf-lib/src/core/errors.ts:31:5) at PDFContext.lookup (/app/node_modules/pdf-lib/src/core/PDFContext.ts:166:11) at PDFDict.lookup (/app/node_modules/pdf-lib/src/core/objects/PDFDict.ts:143:38) at PDFWidgetAnnotation.PDFAnnotation.getAppearances (/app/node_modules/pdf-lib/src/core/annotation/PDFAnnotation.ts:112:18) at PDFTextField.needsAppearancesUpdate (/app/node_modules/pdf-lib/src/api/form/PDFTextField.ts:764:16) at PDFForm.updateFieldAppearances (/app/node_modules/pdf-lib/src/api/form/PDFForm.ts:646:17) at PDFDocument. (/app/node_modules/pdf-lib/src/api/PDFDocument.ts:1284:22) at step (/app/node_modules/pdf-lib/node_modules/tslib/tslib.js:141:27) at Object.next (/app/node_modules/pdf-lib/node_modules/tslib/tslib.js:122:57) at /app/node_modules/pdf-lib/node_modules/tslib/tslib.js:115:75 at new Promise () at Object.__awaiter (/app/node_modules/pdf-lib/node_modules/tslib/tslib.js:111:16) at PDFDocument.save (/app/node_modules/pdf-lib/cjs/api/PDFDocument.js:1243:24)

What did you expect to happen?

I can get Buffer data from PDF fillable files.

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

  • [X] My report includes a Short, Self Contained, Correct (Compilable) Example.
  • [X] I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

No response

prd-phuc-nim avatar Nov 24 '22 05:11 prd-phuc-nim

Yep, having the same issue with some fillable documents. It seems almost random, since some fillable multi-page documents do not work for me, others do. I do not use .flatten(). Didn't have such a problem for a long time using this library to fill in forms in PDFs, but now issues with certain files. For me the error also occurs on .save().

arvo95 avatar Feb 13 '23 17:02 arvo95

Hi, I recently had this same issue and solved it by passing a parameter when saving the pdf

const resultBytes = await pdfDoc.save({ updateFieldAppearances: false });

Hope this helps y'all.

chrisrodz avatar Feb 23 '23 16:02 chrisrodz

Hi, I recently had this same issue and solved it by passing a parameter when saving the pdf

const resultBytes = await pdfDoc.save({ updateFieldAppearances: false });

Hope this helps y'all.

Are there any side effects you see?

yakupteke avatar Mar 13 '23 11:03 yakupteke

@chrisrodz thanks for the suggestion.

I believe, however, that this will not help if you're filling form fields (like I am). As the config param implies, it does not show the value as changing when it saves the PDF, though the text does appear if you open the PDF in Adobe Reader and focus on the field.

I'm now trying to figure out a work around that allows me to omit that config param.

omgreenfield avatar Mar 20 '23 23:03 omgreenfield

@chrisrodz as @omgreenfield , it does not show the value as changing when saving, so some readers (such as the Preview on Mac) show the field as empty even though it is filled in.

jeanyves-yang avatar Jun 15 '23 16:06 jeanyves-yang