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

Render Encrypted Documents using pdf-lib js library

Open Anushka-Touchcore opened this issue 1 year ago • 6 comments

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

  • [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

Anushka-Touchcore avatar Feb 23 '24 05:02 Anushka-Touchcore

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.

themegabyte avatar Apr 20 '24 05:04 themegabyte

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.

Sharcoux avatar May 23 '24 11:05 Sharcoux

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 ?

Jan-Chien avatar May 24 '24 06:05 Jan-Chien

@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)

themegabyte avatar May 27 '24 18:05 themegabyte

Why is this project not maintained anymore?

sayo96 avatar Jun 12 '24 13:06 sayo96