Hopding / pdf-lib

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

Render Encrypted Documents using pdf-lib js library #1601

Open Anushka-Touchcore opened 4 months ago

Anushka-Touchcore commented 4 months ago

What were you trying to do?

Hi,

I am trying to render encrypted document using pdf-js library on UI. However, it is throwing the following error:

pdf-lib.js:6475 Uncaught (in promise) Error: Input document to PDFDocument.load is encrypted. You can use PDFDocument.load(..., { ignoreEncryption: true }) if you wish to load the document anyways.

How did you attempt to do it?

I tried adding { ignoreEncryption: true } property to handle this scenario.

What actually happened?

This is just preventing the exception thrown by the library, but is not rendering the document. Is there any way we can render the encrypted document using pdf-lib js library?

What did you expect to happen?

I am expecting the encrypted pdf to be rendered using pdf-lib js library.

Note: The PDF is rendered perfectly using Postman, I am facing this issue only when rendering the pdf using pdf-lib js library.

How can we reproduce the issue?

We can try to render encrypted pdf using pdf-lib library.

Attached is the PDF file which we are trying to render. ICF TEMP.pdf

Code:

Javascript:

const pdfDoc: any = await PDFDocument.load(existingPdfBytes, { ignoreEncryption: true }); const pdfDataUri: any = await pdfDoc.saveAsBase64({ dataUri: true }); pdfBase64.value = pdfDataUri; document.getElementById('view-pdf').src = pdfDataUri + '#toolbar=0&navpanes=0';

HTML

Version

1.17.1

What environment are you running pdf-lib in?

Browser

Checklist

Additional Notes

No response

themegabyte commented 2 months ago

I have used this tool to decrypt: https://smallpdf.com/unlock-pdf#r=unlock

It has worked well for me so far with pdf-lib.

Sharcoux commented 1 month ago

We've added support for document decryption and other features on our fork: @cantoo/pdf-lib

The project here is not maintained anymore, so you'd probably want to switch.

Jan-Chien commented 1 month ago

I have used this tool to decrypt: https://smallpdf.com/unlock-pdf#r=unlock

It has worked well for me so far with pdf-lib.

thanks! it is very cool, but do you know how to use it in code like javascript ?

themegabyte commented 1 month ago

@Jan-Chien This PR is worth following: https://github.com/cantoo-scribe/pdf-lib/pull/54

It looks like it uses code from this fork: https://github.com/PhakornKiong/pdf-lib/tree/dev/DocEncrypt

perhaps you can try to build from this fork and load your PDF. It looks like if the document is encrypted, it will attempt to decrypt it. (looking at the written tests)

sayo96 commented 4 weeks ago

Why is this project not maintained anymore?