diff --git a/doc/Usage.md b/doc/Usage.md index 787c79fe..5967e253 100644 --- a/doc/Usage.md +++ b/doc/Usage.md @@ -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] - ]; } ``` diff --git a/samples/bugs/Issue427.pdf b/samples/bugs/Issue427.pdf new file mode 100644 index 00000000..15e04e97 Binary files /dev/null and b/samples/bugs/Issue427.pdf differ diff --git a/tests/PHPUnit/Integration/PageTest.php b/tests/PHPUnit/Integration/PageTest.php index 33751e59..93b76f1d 100644 --- a/tests/PHPUnit/Integration/PageTest.php +++ b/tests/PHPUnit/Integration/PageTest.php @@ -41,6 +41,7 @@ use Smalot\PdfParser\Element\ElementMissing; use Smalot\PdfParser\Font; use Smalot\PdfParser\Page; +use TypeError; class PageTest extends TestCase { @@ -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. *