Skip to content

feat(rectangle): add width and height helpers - #317

Merged
PrinsFrank merged 1 commit into
PrinsFrank:mainfrom
vitormattos:feature/page-dimensions-api
May 13, 2026
Merged

PrinsFrank merged 1 commit into
PrinsFrank:mainfrom
vitormattos:feature/page-dimensions-api

Conversation

@vitormattos

@vitormattos vitormattos commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds helper methods to compute rectangle width and height directly from rectangle coordinates.

Usage

$document = (new PdfParser())->parseFile('sampleFile.pdf');

foreach ($document->getPages() as $page) {
    $rectangle = $page->getCropBox() ?? $page->getMediaBox();
    $width = $rectangle?->getWidth();
    $height = $rectangle?->getHeight();
}

@vitormattos

Copy link
Copy Markdown
Contributor Author

@PrinsFrank ready for review.

@PrinsFrank PrinsFrank left a comment •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for the PR and the effort you've put into this!

I think the implementation is a bit too focussed on a specific use case, but we can get this to be generic! It looks like your end goal is to get the crop box of a page, and if it doesn't exist to get the mediabox.

In the end, the code here mostly boils down to three things:

  • Traverse parents for inheritable keys
  • Resolve values for references by retrieving the referenced object and resolving their type.
  • Retrieving width/height for Rectangles

All three are now implemented with a specific use case in mind, but are patterns that could be reused when we implement them according to spec. I think both would be great additions to this library, solving these problems generically, and all three would qualify for their own PRs.

Thanks again for the PR, and seeing the way you implemented this uncovered some great insights!

What do you think?

Comment thread src/Document/Object/Decorator/Page.php Resolved
Comment thread src/Document/Object/Decorator/Page.php Resolved
Comment thread tests/Unit/PdfParserTest.php Resolved
Comment thread src/Document/Object/Decorator/Page.php Resolved
Comment thread src/Document/Object/Decorator/Page.php Resolved
Comment thread src/Document/Object/Decorator/Page.php Resolved
@vitormattos

Copy link
Copy Markdown
Contributor Author

I refactored the PR to make those parts generic rather than page-specific:

  • Parent traversal for inheritable keys now lives in Dictionary (with explicit inheritable-key metadata in DictionaryKey).
  • Reference resolution is handled generically in Dictionary.
  • Width/height logic is centralized in Rectangle (getWidth() / getHeight()).

Page-level code now only consumes dictionary-level APIs for the CropBox/MediaBox fallback flow.

I also moved the dimension tests to align with your testing feedback.

If you prefer, these changes could be split into smaller PRs. Since this PR was already open and under review, I focused the changes on addressing your comments with the smallest coherent end-to-end update. If you prefer, I can split follow-up refinements into separate PRs after this one.

@vitormattos
vitormattos requested a review from PrinsFrank May 7, 2026 01:40
@vitormattos

Copy link
Copy Markdown
Contributor Author

I also changed the PR description after the review.

@PrinsFrank PrinsFrank left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

I only have a few hours per evening to work on this library. Looking at this PR, I think it warrants some rearchitecturing of the Dictionary class and how to traverse parent/child dictionaries. I won't be able to answer that question tonight.

Just to get some context: How many files do you have were you need to traverse Dictionaries for Cropboxes/MediaBoxes to get a definitive answer?

Comment thread src/Document/Dictionary/Dictionary.php Resolved
Comment thread src/Document/Dictionary/Dictionary.php Resolved
@vitormattos

Copy link
Copy Markdown
Contributor Author

I only have a few hours per evening to work on this library. Looking at this PR, I think it warrants some rearchitecturing of the Dictionary class and how to traverse parent/child dictionaries. I won't be able to answer that question tonight.

Just to get some context: How many files do you have were you need to traverse Dictionaries for Cropboxes/MediaBoxes to get a definitive answer?

Good question, but after your implementation, rearchitecturing Dictionary is no longer necessary.
Your page-level getInheritableValue() approach is much cleaner, and this PR now simply builds on top of it.
So we don't need to traverse Dictionary for CropBoxes/MediaBoxes anymore, Document::getPagesDimensions() just calls $page->getCropBox() / $page->getMediaBox() directly.
I already made a push rebased with main branch.

Comment thread src/Document/Document.php Resolved
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
@vitormattos
vitormattos force-pushed the feature/page-dimensions-api branch from d9172c1 to a4a101d Compare May 10, 2026 19:21
@vitormattos vitormattos changed the title feat: add per-page dimensions API with CropBox/MediaBox support feat(rectangle): add width and height helpers May 10, 2026
@vitormattos
vitormattos requested a review from PrinsFrank May 10, 2026 19:32

@PrinsFrank PrinsFrank left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution! I'll release a new version including this change soon!

@PrinsFrank
PrinsFrank merged commit 05181df into PrinsFrank:main May 13, 2026
21 checks passed
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.

2 participants