From 3d5b91b338b53673b2f23e649e9e400706676a7c Mon Sep 17 00:00:00 2001 From: christian cannata Date: Fri, 4 Nov 2016 22:47:21 +0100 Subject: [PATCH 1/2] ignore errors when not find headers --- src/Smalot/PdfParser/Document.php | 10 ++++++---- src/Smalot/PdfParser/Header.php | 14 +++++++------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Smalot/PdfParser/Document.php b/src/Smalot/PdfParser/Document.php index e913e014..9c75894e 100644 --- a/src/Smalot/PdfParser/Document.php +++ b/src/Smalot/PdfParser/Document.php @@ -119,13 +119,15 @@ protected function buildDetails() // Extract document info if ($this->trailer->has('Info')) { /** @var Object $info */ - $info = $this->trailer->get('Info'); - $details = $info->getHeader()->getDetails(); + $info = $this->trailer->get('Info'); + if ($info !== null) { + $details = $info->getHeader()->getDetails(); + } } // Retrieve the page count try { - $pages = $this->getPages(); + $pages = $this->getPages(); $details['Pages'] = count($pages); } catch (\Exception $e) { $details['Pages'] = 0; @@ -216,7 +218,7 @@ public function getPages() /** @var Pages $object */ $object = $this->objects[$id]->get('Pages'); if (method_exists($object, 'getPages')) { - $pages = $object->getPages(true); + $pages = $object->getPages(true); return $pages; } } diff --git a/src/Smalot/PdfParser/Header.php b/src/Smalot/PdfParser/Header.php index 01d6dbca..1156e695 100644 --- a/src/Smalot/PdfParser/Header.php +++ b/src/Smalot/PdfParser/Header.php @@ -53,8 +53,8 @@ class Header protected $elements = null; /** - * @param Element[] $elements List of elements. - * @param Document $document Document. + * @param Element[] $elements List of elements. + * @param Document $document Document. */ public function __construct($elements = array(), Document $document = null) { @@ -99,7 +99,7 @@ public function getElementTypes() */ public function getDetails($deep = true) { - $values = array(); + $values = array(); $elements = $this->getElements(); foreach ($elements as $key => $element) { @@ -112,7 +112,7 @@ public function getDetails($deep = true) $values[$key] = $element->getDetails(); } } elseif ($element instanceof Element) { - $values[$key] = (string) $element; + $values[$key] = (string)$element; } } @@ -164,7 +164,7 @@ protected function resolveXRef($name) $object = $this->document->getObjectById($obj->getId()); if (is_null($object)) { - throw new \Exception('Missing object reference #' . $obj->getId() . '.'); + return null; } // Update elements list for future calls. @@ -175,9 +175,9 @@ protected function resolveXRef($name) } /** - * @param string $content The content to parse + * @param string $content The content to parse * @param Document $document The document - * @param int $position The new position of the cursor after parsing + * @param int $position The new position of the cursor after parsing * * @return Header */ From 161b43843062188e030f172ef8e203292b3c4ab9 Mon Sep 17 00:00:00 2001 From: christian cannata Date: Fri, 4 Nov 2016 23:16:19 +0100 Subject: [PATCH 2/2] fix tests --- src/Smalot/PdfParser/Header.php | 12 ++++++------ src/Smalot/PdfParser/Tests/Units/Header.php | 13 +++++-------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/Smalot/PdfParser/Header.php b/src/Smalot/PdfParser/Header.php index 1156e695..3b2f0741 100644 --- a/src/Smalot/PdfParser/Header.php +++ b/src/Smalot/PdfParser/Header.php @@ -53,8 +53,8 @@ class Header protected $elements = null; /** - * @param Element[] $elements List of elements. - * @param Document $document Document. + * @param Element[] $elements List of elements. + * @param Document $document Document. */ public function __construct($elements = array(), Document $document = null) { @@ -99,7 +99,7 @@ public function getElementTypes() */ public function getDetails($deep = true) { - $values = array(); + $values = array(); $elements = $this->getElements(); foreach ($elements as $key => $element) { @@ -112,7 +112,7 @@ public function getDetails($deep = true) $values[$key] = $element->getDetails(); } } elseif ($element instanceof Element) { - $values[$key] = (string)$element; + $values[$key] = (string) $element; } } @@ -175,9 +175,9 @@ protected function resolveXRef($name) } /** - * @param string $content The content to parse + * @param string $content The content to parse * @param Document $document The document - * @param int $position The new position of the cursor after parsing + * @param int $position The new position of the cursor after parsing * * @return Header */ diff --git a/src/Smalot/PdfParser/Tests/Units/Header.php b/src/Smalot/PdfParser/Tests/Units/Header.php index 008f1230..01566a87 100644 --- a/src/Smalot/PdfParser/Tests/Units/Header.php +++ b/src/Smalot/PdfParser/Tests/Units/Header.php @@ -118,13 +118,9 @@ public function testGet() $this->assert->object($header->get('SubType'))->isInstanceOf('\Smalot\PdfParser\Element\ElementName'); $this->assert->object($header->get('Font'))->isInstanceOf('\Smalot\PdfParser\Page'); $this->assert->object($header->get('Image'))->isInstanceOf('\Smalot\PdfParser\Element\ElementMissing'); + $resources = $header->get('Resources'); - try { - $resources = $header->get('Resources'); - $this->assert->boolean(true)->isEqualTo(false); - } catch (\Exception $e) { - $this->assert->exception($e)->hasMessage('Missing object reference #8_0.'); - } + $this->assert->variable($resources)->isNull(); } public function testResolveXRef() @@ -138,11 +134,12 @@ public function testResolveXRef() $this->assert->object($header->get('Font'))->isInstanceOf('\Smalot\PdfParser\Object'); + $header=$header->get('Resources'); try { - $this->assert->object($header->get('Resources'))->isInstanceOf('\Smalot\PdfParser\Element\ElementMissing'); + $this->assert->variable($header)->isInstanceOf('\Smalot\PdfParser\Element\ElementMissing'); $this->assert->boolean(true)->isEqualTo(false); } catch (\Exception $e) { - $this->assert->exception($e)->hasMessage('Missing object reference #8_0.'); + $this->assert->variable($header)->isNull(); } } }