pymupdf/PyMuPDF

TypeError in utils.py scrub(): annot.update_file(buffer=...) is invalid

Closed this issue · 1 comments

Description of the bug

When using doc.scrub() from pymupdf.utils, it fails on PDFs with file attachments due to an outdated call to:

annot.update_file(buffer=b" ")

In PyMuPDF, this results in:

TypeError: update_file() got an unexpected keyword argument 'buffer'

Expected Behavior
The correct argument name in current versions is buffer_ (with an underscore), or should be passed positionally.

A Solution
Please update utils.py to:

annot.update_file(buffer_=b" ")

How to reproduce the bug

Use doc.scrub() on a PDF with an embedded file annotation.

Observe the crash.

Traceback (most recent call last):
  File "/var/task/watermark_utils.py", line 40, in sub_pdf
    doc.scrub()
    ^^^^^^^^^^^
  File "/opt/python/pymupdf/utils.py", line 4372, in scrub
    annot.update_file(buffer=b" ")  # set file content to empty
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Annot.update_file() got an unexpected keyword argument 'buffer'

To create simple PDF that will cause the crash

    # Create a new PDF
    doc = fitz.open()
    page = doc.new_page()

    # Add some text
    text = "This PDF contains a file attachment annotation."
    page.insert_text((72, 72), text, fontsize=12)

    # Path to the file you want to attach (must exist)
    file_to_attach = "tests/fixtures/annotation_sample.txt"

    # Create a sample file if it doesn't exist
    if not os.path.exists(file_to_attach):
        with open(file_to_attach, "w") as f:
            f.write("This is a sample attachment file.")

    # Read file as bytes
    with open(file_to_attach, "rb") as f:
        file_bytes = f.read()

    # Define annotation position (rect or point)
    annot_pos = fitz.Rect(72, 100, 92, 120)  # PushPin icon rectangle

    # Add the file attachment annotation
    page.add_file_annot(
        annot_pos,
        file_bytes,
        "sample.txt",
        "sample.txt",           # ufilename (optional)
        "A test attachment file.",  # desc (optional)
        "PushPin"               # icon (optional)
    )

    # Save the PDF
    original_pdf_path = "tests/fixtures/annotation_sample.pdf"
    doc.save(original_pdf_path)
    doc.close()

annotation_test.pdf

PyMuPDF version

1.26.1

Operating system

Linux

Python version

3.12

Fixed in PyMuPDF-1.26.4.