Skip to content

Commit

Permalink
Merge pull request #126 from christiancannata/ignore-error-headers
Browse files Browse the repository at this point in the history
Fix for Issue #38: When there is no object reference, continue parsing PDF file
  • Loading branch information
smalot authored Nov 5, 2016
2 parents 3cd6697 + 161b438 commit 34fb40f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
10 changes: 6 additions & 4 deletions src/Smalot/PdfParser/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 5 additions & 8 deletions src/Smalot/PdfParser/Tests/Units/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
}
}
}

0 comments on commit 34fb40f

Please sign in to comment.