Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"fitz.mupdf.FzErrorArgument: code=4: source object number out of range" after "add_redact_annot" #3408

Closed
bsdcpp opened this issue Apr 23, 2024 · 3 comments
Labels
documentation issue not a bug not a bug / user error / unable to reproduce

Comments

@bsdcpp
Copy link

bsdcpp commented Apr 23, 2024

Description of the bug

Hi, there.
Here's the situation: I'm trying to hide certain content in a PDF, so I used the following code. It seems like the code can only handle PDF files with one page, as it throws an error similar to the one mentioned above when dealing with more than one page.

from __future__ import print_function
import fitz, sys

src = fitz.open(sys.argv[1])
doc = fitz.open()

for spage in src:
    r = spage.rect
    redact_area = fitz.Rect(100, 100, 200, 200)
    spage.add_redact_annot(redact_area)
    spage.apply_redactions()  # clears areas to exclude

    page = doc.new_page(-1, width=r.width, height=r.height)
    page.show_pdf_page(
        page.rect,
        src,
        spage.number,
        clip=r,
    )

doc.save("output.pdf", garbage=4, deflate=True)

The code runs without errors if I comment out these two lines:

spage.add_redact_annot((0, 0, 100, 100))
spage.apply_redactions()  # clears areas to exclude

How to reproduce the bug

Any PDF file can be used to test the error code. I've tried several versions of PyMuPDF, and they all have this problem.
problem.pdf
No_problem.pdf

PyMuPDF version

1.24.2

Operating system

MacOS, Linux

Python version

3.11, 3.8

@JorjMcKie
Copy link
Collaborator

Applying redactions massively changes the structure of a PDF page.

As always in similar cases, this should be done outside a loop that iterates over the same objects. The following modified script works:

import fitz, sys

src = fitz.open(sys.argv[1])
doc = fitz.open()
for spage in src:
    redact_area = fitz.Rect(100, 100, 200, 200)
    spage.add_redact_annot(redact_area)
    spage.apply_redactions()  # clears areas to exclude

for spage in src:
    w, h = spage.rect.br
    page = doc.new_page(width=w, height=h)
    page.show_pdf_page(page.rect, src, spage.number, clip=spage.rect)

doc.ez_save(src.name.replace(".pdf", "-redacted.pdf"), garbage=4)

@JorjMcKie
Copy link
Collaborator

We will consider documenting an according caveat.

@JorjMcKie JorjMcKie added documentation issue not a bug not a bug / user error / unable to reproduce labels Apr 24, 2024
@bsdcpp
Copy link
Author

bsdcpp commented Apr 24, 2024

Thanks a lot for your answer, amazing, it works.

@bsdcpp bsdcpp closed this as completed Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation issue not a bug not a bug / user error / unable to reproduce
Projects
None yet
Development

No branches or pull requests

2 participants