-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Encryption and Decryption of PDFs examples in documentation remove attachments, links, ... #2543
Copy link
Copy link
Closed
Description
I want to decrypt a PDF document, while keeping the document intact. The documentation gives an example for this (https://pypdf.readthedocs.io/en/stable/user/encryption-decryption.html); but this decrypts the document, then reconstructs it by copying page by page to the new document.
This removes attachments, but also the table of contents, the links in the document pointing to other pages, ...
I would like an approach where the encrypted data is decrypted 'in-place', keeping the document structure intact.
Environment
Which environment were you using when you encountered the problem?
$ python -m platform
Linux-6.5.0-26-generic-x86_64-with-glibc2.35
$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==4.1.0, crypt_provider=('cryptography', '42.0.5'), PIL=none
(modified by me to add support for the pubsec decryptor, but this doesn't affect anything)Code + PDF
This is a minimal, complete example that shows the issue; it does not use encryption, but it has the same problem as the sample that does use encryption.
from pypdf import PdfReader, PdfWriter
reader = PdfReader("PN7160_PN7161.pdf")
writer = PdfWriter()
for idx, page in enumerate(reader.pages):
writer.add_page(page)
print("Saving to file...")
# Save the new PDF to a file
with open("out.pdf", "wb") as f:
writer.write(f)Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels