Skip to content

Encryption and Decryption of PDFs examples in documentation remove attachments, links, ... #2543

@redfast00

Description

@redfast00

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)

PN7160_PN7161.pdf

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions