Skip to content

Render content outside the CropBox with force_mediabox - #1435

Open
ainuo-AI wants to merge 1 commit into
jsvine:developfrom
ainuo-AI:codex/fix-mediabox-rendering
Open

ainuo-AI wants to merge 1 commit into
jsvine:developfrom
ainuo-AI:codex/fix-mediabox-rendering

Conversation

@ainuo-AI

Copy link
Copy Markdown

Page.to_image(force_mediabox=True) currently renders only the CropBox, then pads the image to the MediaBox with black pixels. Actual PDF content outside the CropBox is lost. In the new synthetic fixture, a red rectangle outside the CropBox disappears while a blue rectangle inside remains visible.

Pass pdfminer's unrotated, inherited MediaBox to PDFium before rendering, then compute the image scale and any derived-page crop from the box actually rendered. Using the parsed box also avoids relying on PDFium box getters that do not inherit page-tree boxes.

The new 812-byte PDF has four page rotations, with both boxes inherited from their parent. Twenty cases check actual content pixels, default/resolution/width/height sizing, derived-page cropping, and the default CropBox rendering. Eighteen of the twenty cases fail on unchanged develop; the two default-rendering controls already pass. A binary comment prevents Git's automatic newline conversion from invalidating PDF byte offsets on Windows.

The existing fractional-box fixture now renders at 2226 x 2920 pixels at 72 DPI, matching its rotated 2225.2 x 2919.69 point MediaBox and an independent Poppler render. Its old 2227 x 2923 expectation came from scaling the cropped bitmap. The corrected assertion also fails when run with the original display.py.

Validation on Windows, CPython 3.12.10:

  • All 37 display tests pass with pypdfium2 5.15.2 and with the minimum supported 5.9.0.
  • Full suite: 183 passed, 10 failed. The remaining failures also occur on unchanged develop: six repair tests lack Ghostscript, and four CLI tests encounter Windows newline differences. No new failures appear.
  • Black (all 37 Python files checked individually), isort, Flake8, strict mypy, and git diff --check pass.
  • python setup.py build sdist passes.
  • The generated fixture was independently rendered with Poppler, and the before/after images were visually inspected.

Changelog and contributor credit are included. No new dependencies are required. Linux and other Python versions have not been verified locally.

AI assistance was used to prepare this change and its regression tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant