PyMuPDF icon indicating copy to clipboard operation
PyMuPDF copied to clipboard

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

Open mac5yver opened this issue 1 year ago • 1 comments

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

mac5yver avatar Aug 08 '24 22:08 mac5yver

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

GrandmaMeemaw avatar Sep 24 '24 07:09 GrandmaMeemaw

We ae sorry to say that we cannot doanythng about this. The file behaves as expected using any PDF reader from Foxit, Nitro, PDF-XChange.

JorjMcKie avatar Mar 16 '25 21:03 JorjMcKie