Skip to content

Commit

Permalink
chore: update documentation
Browse files Browse the repository at this point in the history
Follow up of issue smalot#427 using the suggested code at issue smalot#472.

Notes:
Have PDF files that the pdfparser can't get the MediaBox, as fallback to
get the page dimension is possible to use the BBox (Bounding Box).

The Bounding Box could not have the real dimension of a page put is a
solution to have a value that could be the page dimension at cases that
isn't possible to found the MediaBox.

Signed-off-by: Vitor Mattos <[email protected]>
  • Loading branch information
vitormattos committed Jan 24, 2025
1 parent f44ada0 commit 2edc3ac
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
19 changes: 15 additions & 4 deletions doc/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,22 @@ foreach ($pages as $page) {
if (!isset($details['MediaBox'])) {
$pages = $pdf->getObjectsByType('Pages');
$details = reset($pages)->getHeader()->getDetails();
if (!isset($details['MediaBox'])) {
$objects = $page->getXObjects();
if (isset($objects[0])) {
$details = $objects[0]->getHeader()->getDetails();
if (isset($details['BBox'])) {
$details['MediaBox'] = $details['BBox'];
}
}
}
}
if (isset($details['MediaBox'])) {
$mediaBox[] = [
'width' => $details['MediaBox'][2],
'height' => $details['MediaBox'][3]
];
}
$mediaBox[] = [
'width' => $details['MediaBox'][2],
'height' => $details['MediaBox'][3]
];
}
```

Expand Down
Binary file added samples/bugs/Issue427.pdf
Binary file not shown.
37 changes: 37 additions & 0 deletions tests/PHPUnit/Integration/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Smalot\PdfParser\Element\ElementMissing;
use Smalot\PdfParser\Font;
use Smalot\PdfParser\Page;
use TypeError;

class PageTest extends TestCase
{
Expand Down Expand Up @@ -905,6 +906,42 @@ public function testIssue629WithDataTmFontInfo(): void
$this->assertEquals('F2', $dataTm[0][2]);
}

public function testIssue427GetPageDimensions(): void
{
$filename = $this->rootDir.'/samples/bugs/Issue427.pdf';
$parser = $this->getParserInstance();
$document = $parser->parseFile($filename);
$pages = $document->getPages();
$notFound = 0;
foreach ($pages as $page) {
$details = $page->getDetails();
if (!isset($details['MediaBox'])) {
$fallbackPages = $document->getObjectsByType('Pages');
$details = reset($fallbackPages)->getHeader()->getDetails();
if (!isset($details['MediaBox'])) {
try {
$objects = $page->getXObjects();
if (isset($objects[0])) {
$details = $objects[0]->getHeader()->getDetails();
if (isset($details['BBox'])) {
$this->assertArrayHasKey('BBox', $details);
$this->assertArrayHasKey(2, $details['BBox']);
$this->assertArrayHasKey(3, $details['BBox']);
continue;
}
}
$notFound++;
continue;
} catch (TypeError $e) {
$notFound++;
continue;
}
}
}
}
$this->assertEquals(count($pages) - 1, $notFound);
}

/**
* Data TM font info is NOT included.
*
Expand Down

0 comments on commit 2edc3ac

Please sign in to comment.