pymupdf / PyMuPDF

PyMuPDF is a high performance Python library for data extraction, analysis, conversion & manipulation of PDF (and other) documents.
https://pymupdf.readthedocs.io
GNU Affero General Public License v3.0
5.33k stars 509 forks source link

PDFs saved with encryption cannot be saved in Acrobat Pro with "Bad parameter" error #3762

Open mac5yver opened 2 months ago

mac5yver commented 2 months ago

Description of the bug

All attempts at saving a pdf with encryption and an owner_pw result in a PDF that will open in Acrobat Pro (2024.002.20964) on macOS, but after adding, for example, a comment, Acrobat cannot save the document. Acrobat throws the following error: "The document could not be saved. Bad parameter."

How to reproduce the bug

Example file: protected.pdf

Created with this code:

def protect_pdf():
    perm = int(
        pymupdf.PDF_PERM_ACCESSIBILITY
        | pymupdf.PDF_PERM_PRINT
        | pymupdf.PDF_PERM_PRINT_HQ
        | pymupdf.PDF_PERM_ANNOTATE
        | pymupdf.PDF_PERM_ASSEMBLE
        | pymupdf.PDF_PERM_COPY
        | pymupdf.PDF_PERM_FORM
    )
    doc1 = pymupdf.open()
    doc1.new_page()
    doc1.save(
        "protected.pdf",
        encryption=pymupdf.PDF_ENCRYPT_AES_256,
        owner_pw="owner",
        permissions=perm,
        garbage=4,
        deflate=True,
        clean=True,
    )
    doc1.close()

protect_pdf()

I've tried many variations of garbage collection, compression, and cleaning. All PDFs I create open in Acrobat Pro, but they will not save. Acrobat offers to Save As, but then fails with this error: "The document could not be saved. There was a problem reading this document (21)."

Removing the encryption, owner_pw, and permissions arguments from the save function results in a PDF that works fine in Acrobat, but is not protected.

Of note, the PDFs open and save again just fine in PDF X-Change on Windows.

Any help is much appreciated.

PyMuPDF version

1.24.9

Operating system

MacOS

Python version

3.12

GrandmaMeemaw commented 3 weeks ago

same problem here. encrypted PDF using pymupdf cannot be saved, getting the same error message: "The document could not be saved. There was a problem reading this document (21)."

The file could be opened, and the Document Restriction settings are all correct based on the encryption parameters used. The errors occurs whenever I tried to save the file after adding a comment to the file. I also tried just to save the PDF using Acrobat Pro, same error message.

test files were created using MS Word. Input file: testfile.pdf Encrypted file: testfile_encrypted.pdf