-
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. EnvironmentWhich 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 + PDFThis 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) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Create a backup of the PDF, then switch these:
|
Beta Was this translation helpful? Give feedback.
-
@j-t-1 that works! Should I make a PR to replace that in the documentation? |
Beta Was this translation helpful? Give feedback.
-
Could do, currently it is not obvious that adding each page is insufficient to have equivalency; I had a similar problem #2485. |
Beta Was this translation helpful? Give feedback.
Create a backup of the PDF, then switch these:
writer = PdfWriter(clone_from=reader)