Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
6.84k stars 652 forks source link

Expected instance of PDFDict, but got instance of undefined #1260

Open dinobal opened 2 years ago

dinobal commented 2 years ago

What were you trying to do?

Getting indices, pages or a page by index throws this error. Cant share the document but because of sensitive data. It has Page Extraction: Not Allowed and Document Assembly: Not Allowed but that doesn't prevent other PDFs working just fine.

How did you attempt to do it?

Simple and straight forward

const attachedPDF = await PDFDocument.load(existingPdfBytes, { ignoreEncryption: true });
const pageIndices = attachedPDF.getPageIndices();

What actually happened?

Works most of the time but invoice PDFs from a specific client are not ok. It always throws Expected instance of PDFDict, but got instance of undefined when this const pageIndices = attachedPDF.getPageIndices(); or this const pages = attachedPDF.getPages(); is executed...

If I open it in Edge and add some text and save it as changed doc. The changed doc works without issues.

What did you expect to happen?

Get information about pages.

How can we reproduce the issue?

I can not make the document public as its an invoice but can ask for permission to send it privately if necessary. Editing information fixes the issue which makes it hard to erase sensitive data. e.g. Editing in MS Edge (just adding text) fixes the issue.

Version

1.17.1

What environment are you running pdf-lib in?

Browser

Checklist

Additional Notes

No response

dreinon commented 2 years ago

+1

quiquelhappy commented 1 year ago

+1

JeffJankowski commented 1 year ago

+1

iGitScor commented 1 year ago

Hello,

I have the same issue and can't figure out what's the problem. Of course, it's a sensitive pdf file so I can't share it

const pdf = await PDFDocument.load(buffer, { ignoreEncryption: true });
const pageCount = pdf.getPageCount();
github-anis-snoussi commented 1 year ago

+1

nicolofranceschi commented 1 year ago
mojoaxel commented 1 year ago

Here is an issue with an example PDF that causes this error: https://github.com/nbesli/pdf-merger-js/issues/120

I hope this helps!

pheidler commented 1 year ago

I was having the same issue, and it was due to PDFs not being PDF/A compliant.

https://github.com/Hopding/pdf-lib/issues/623#issuecomment-724654427

SimonRenault86 commented 9 months ago

+1

longgtruong commented 6 months ago

+1

PastaMista commented 6 months ago

+1

andefran commented 4 months ago

+1

davidstackio commented 3 months ago

This is likely due to the pdfs being encrypted. You can tell by the (SECURED) message when opening in a pdf reader app (not the browser). See: https://www.quora.com/How-would-I-know-if-a-PDF-document-is-password-protected

I'm having the same issue with my encrypted/secured pdfs.

NOTE: Just because it's a secure pdf, doesn't mean it necessarily requires a password. Here is an example from my PDF:

image (screenshot from Foxit Reader)

Depending on your use case you may be able to use a fork of this repo as described here since that appears to have encrypted pdf support: https://github.com/Hopding/pdf-lib/issues/1616

I haven't figured out a good solution for this at scale, but if it's a small batch of pdfs, you can probably open them in a pdf reader, then print as a pdf to make a new one, then run your processing on that.