pymupdf issue: https://github.com/pymupdf/PyMuPDF/issues/4415 problem file: https://github.com/user-attachments/files/19503231/test.pdf Page 0 has an array of /Contents objects "/Contents [ 10 0 R 9 0 R 8 0 R 7 0 R ]" with concatenated content: q 1 0 0 1 0 841.67999267 cm 1 0 0 -1 0 0 cm q 0 0 m 0 841.67999267 l 594.7199707 841.67999267 l 594.7199707 0 l h W n /OC /OCG_5a60b95a061a41958a6919570b57d483 BDC /DeviceRGB CS 0 0 0 SC 0.72000002 w 1 J 1 j [] 0 d 135.7000122 612.1833496 m 298.27334594 419.47000122 l S 0 0 0 SC 444.87686157 422.20999145 m 444.87686157 502.84192504 378.90527954 568.81350708 298.27334594 568.81350708 c 217.64141235 568.81350708 151.66983032 502.84192504 151.66983032 422.20999145 c 151.66983032 341.57805786 217.64141235 275.60647583 298.27334594 275.60647583 c 378.90527954 275.60647583 444.87686157 341.57805786 444.87686157 422.20999145 c S EMC Q So the q/Q-counts are clearly unbalanced (2, 1), none-the-less function "pdf_count_q_balance" returns (0, 0).
This is because the pdf interpreter mechanism automatically inserts extra pops as required. I'll think ways to fix it.
Fix in testing: https://cgit.ghostscript.com/cgi-bin/cgit.cgi/user/robin/mupdf.git/commit/?h=pending&id=a9f6fc77d794f1cefac811c6e73c3352dfa5750b If I don't get to commit it before I go on holiday, then maybe the others can?
Confirming that the issue has been correctly fixed, see comment https://github.com/pymupdf/PyMuPDF/issues/4415#issuecomment-2763595019
Fixed in commit b660c49195081d8bc2766079d261dbbb1eeb08ef Author: Robin Watts <Robin.Watts@artifex.com> Date: Fri Mar 28 19:23:42 2025 +0000 Bug 708393: Fix pdf_count_q_balance. pdf_count_q_balance relies on counting q and Q operations to detect mismatches. Unfortunately, it does this by running the standard PDF interpretation mechanisms, and these attempt to autocorrect for missing Q's at the end of a page. This autocorrection affects our reading. Insert a new processor event that happens before the auto correction, and use that so that pdf_count_q_balance can get the uncorrected values.