Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error while creating a deep clone of decrypted PDFDocument object. #65

Open
2 tasks done
Talha345 opened this issue Aug 23, 2024 · 2 comments
Open
2 tasks done
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@Talha345
Copy link

Talha345 commented Aug 23, 2024

What were you trying to do?

I create a deep clone of the PDFDocument object before applying any changes to it using the following code:

const copiedPdfDoc = await PDFDocument.load(await pdfDoc.save());

This approach has been working reliably for a long time, but after switching to the new fork, I've encountered a problem with certain encrypted PDF files that have been decrypted. For most files, the deep clone works fine. However, when dealing with specific decrypted PDFs, the PDFDocument.catalog in the cloned document is set to PDFInvalidObject instead of the expected PDFCatalog and when I try to do await copiedPdfDoc.save();, I get the following error:

TypeError: this.catalog.Pages is not a function
    at PDFDocument.computePages [as populate] 
    at Cache.access (Cache.js:11:1)
    at PDFDocument.getPages (PDFDocument.js:478:1)
    at PDFDocument.getPageCount (PDFDocument.js:462:1)
    at PDFDocument.save (PDFDocument.js:1147:1)

Document for which the issue occurs:

g-1145.pdf

How did you attempt to do it?

  1. Load the provided PDF document.
  2. Create a deep clone of the document using the code provided above.
  3. Try to save the copied pdf document.

What actually happened?

TypeError: this.catalog.Pages is not a function
    at PDFDocument.computePages [as populate] 
    at Cache.access (Cache.js:11:1)
    at PDFDocument.getPages (PDFDocument.js:478:1)
    at PDFDocument.getPageCount (PDFDocument.js:462:1)
    at PDFDocument.save (PDFDocument.js:1147:1)

What did you expect to happen?

The PDF to be saved properly.

How can we reproduce the issue?

Use the steps above.

Version

2.2.3

What environment are you running pdf-lib in?

Browser

Checklist

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

Additional Notes

After further investigation, it looks like the file gets corrupted upon saving and therefore can't be loaded again correctly even in other PDF viewers. I am using decrypting the file with '' as password in the first place.

@Talha345 Talha345 added bug Something isn't working needs-triage labels Aug 23, 2024
@Talha345 Talha345 changed the title Error while creating a deep clone of encrypted PDFDocument object. Error while creating a deep clone of decrypted PDFDocument object. Aug 24, 2024
@Talha345
Copy link
Author

Talha345 commented Aug 24, 2024

Update: I migrated the decryption logic to use qpdf CLI on the BE and it works like a charm but it would be nice if pdf-lib handles this correctly.

@Sharcoux
Copy link
Collaborator

Sharcoux commented Sep 5, 2024

We might try to have a look, but I'm really not sure we can help on this. I'll mark this as help wanted.

@Sharcoux Sharcoux added help wanted Extra attention is needed and removed needs-triage labels Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants