cantoo-scribe / pdf-lib

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

"Error: Input document to `PDFDocument.load` is encrypted ..." (but `pdf.js` and other readers can read the file without password) #75

Open nicolassanmar opened 2 weeks ago

nicolassanmar commented 2 weeks ago

What were you trying to do?

I was trying to load a document using PDFDocument.load, but it threw this Error. The PDF document can be loaded without any issues on pdf.js and in other PDF readers. Here is a sample document that fails to load here:

sample.pdf

How did you attempt to do it?

I downloaded my document from Adobe Sign, then uploaded it to the webapp where pdf-lib is available. I then wanted to edit it using pdf-lib, for which I loaded the document with PDFDocument.load as I do for any other document. This throws the error:

Error: Input document to `PDFDocument.load` is encrypted. You can use `PDFDocument.load(..., { ignoreEncryption: true })` if you wish to load the document anyways.
    at new EncryptedPDFError (webpack-internal:///./node_modules/.pnpm/pdf-lib@1.17.1/node_modules/pdf-lib/es/api/errors.js:27:24)
    at new PDFDocument (webpack-internal:///./node_modules/.pnpm/pdf-lib@1.17.1/node_modules/pdf-lib/es/api/PDFDocument.js:72:19)
    at Function.eval (webpack-internal:///./node_modules/.pnpm/pdf-lib@1.17.1/node_modules/pdf-lib/es/api/PDFDocument.js:144:47)
    at step (webpack-internal:///./node_modules/.pnpm/tslib@1.14.1/node_modules/tslib/tslib.es6.js:126:23)
    at Object.eval [as next] (webpack-internal:///./node_modules/.pnpm/tslib@1.14.1/node_modules/tslib/tslib.es6.js:107:53)
    at fulfilled (webpack-internal:///./node_modules/.pnpm/tslib@1.14.1/node_modules/tslib/tslib.es6.js:97:58)

Allowing ignoreEncryption does not fix this issue, as it prevents the document from being parsed properly (it returns all blank pages).

What actually happened?

I got the error mentioned above.

What did you expect to happen?

I expected the document to load without any issues into pdf-lib. I expected this to work as I saw this comment here, stating that this fork should have fixed the issue: https://github.com/Hopding/pdf-lib/issues/1601#issuecomment-2126845851

How can we reproduce the issue?

Just run PDFDocument.load() over the document I shared above (which I will share here as well).

4ffefced-d7bc-4b46-bead-28cd1710534b original.pdf

Version

2.2.3

What environment are you running pdf-lib in?

Browser

Checklist

Additional Notes

No response

nicolassanmar commented 2 weeks ago

I think I solved this issue by loading the documents by passing an empty password to the load options:

await PDFDocument.load(pdfBytes, {
            throwOnInvalidObject: true,
            ignoreEncryption: true,
            password: '',
          }),

This is confusing, as I would expect that doing this is not necessary for documents that do not have a password