Description of the bug
We are noticing that Page.clip_to_rect() incorrectly deletes certain graphic elements, even if they fall within the clip box. I've had this for PDFs authored in Adobe Illustrator containing certain kinds of gradients on curved lines - which are expressed in the PDF as Shading elements. I've created a minimal PDF example below.
I don't know enough about the PDF spec internals to be able to debug any further, so let me know if I'm missing anything. clip_to_rect seems to perform perfectly in all other scenarios, it's just these types of graphics.
Thanks for your help!
How to reproduce the bug
Using the minimal PDF attached below, we can see that any clip_to_rect operation that does not include the origin of the page (top left) results in the gradient shading being deleted, even if the gradient is visually in the clipped area:
import pymupdf
def clipped_content(clip):
doc = pymupdf.open("shading-repro.pdf")
doc[0].clip_to_rect(clip)
doc2 = pymupdf.open(stream=doc.tobytes(garbage=3, clean=True))
return b"".join(doc2.xref_stream(x) for x in doc2[0].get_contents())
print("pymupdf", pymupdf.version)
for label, clip in [
("whole page ", pymupdf.Rect(0, 0, 400, 400)),
("right half (holds shading) ", pymupdf.Rect(200, 0, 400, 400)),
("page inset by 1pt ", pymupdf.Rect(1, 1, 399, 399)),
("1x1 at PDF origin, no shading in it", pymupdf.Rect(0, 399, 1, 400)),
]:
content = clipped_content(clip)
print(f" {label} -> sh kept: {b'sh' in content} {content!r}")
This prints:
pymupdf ('1.28.2', '1.28.2', None)
whole page -> sh kept: True
right half (holds shading) -> sh kept: False
page inset by 1pt -> sh kept: False
1x1 at PDF origin, no shading in it -> sh kept: True
The final test also shows that the shading graphic is not deleted when clipping to the origin where the graphic is not present.
shading-repro.pdf
PyMuPDF version
1.28.2
Operating system
Linux
Python version
3.14
Description of the bug
We are noticing that
Page.clip_to_rect()incorrectly deletes certain graphic elements, even if they fall within the clip box. I've had this for PDFs authored in Adobe Illustrator containing certain kinds of gradients on curved lines - which are expressed in the PDF asShadingelements. I've created a minimal PDF example below.I don't know enough about the PDF spec internals to be able to debug any further, so let me know if I'm missing anything.
clip_to_rectseems to perform perfectly in all other scenarios, it's just these types of graphics.Thanks for your help!
How to reproduce the bug
Using the minimal PDF attached below, we can see that any
clip_to_rectoperation that does not include the origin of the page (top left) results in the gradient shading being deleted, even if the gradient is visually in the clipped area:This prints:
The final test also shows that the shading graphic is not deleted when clipping to the origin where the graphic is not present.
shading-repro.pdf
PyMuPDF version
1.28.2
Operating system
Linux
Python version
3.14