Skip to content

Commit

Permalink
Merge pull request #324 from smalot/fix/font-elementmissing
Browse files Browse the repository at this point in the history
Fix error when Font aren't available
  • Loading branch information
j0k3r authored Aug 31, 2020
2 parents 2abf9c8 + cc4c77f commit 5faf073
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Smalot/PdfParser/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public function getFonts()
$resources = $this->get('Resources');

if (method_exists($resources, 'has') && $resources->has('Font')) {
if ($resources->get('Font') instanceof ElementMissing) {
return [];
}

if ($resources->get('Font') instanceof Header) {
$fonts = $resources->get('Font')->getElements();
} else {
Expand Down
32 changes: 32 additions & 0 deletions tests/Integration/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@

namespace Tests\Smalot\PdfParser\Integration;

use Smalot\PdfParser\Document;
use Smalot\PdfParser\Element\ElementMissing;
use Smalot\PdfParser\Font;
use Smalot\PdfParser\Page;
use Tests\Smalot\PdfParser\TestCase;

class PageTest extends TestCase
Expand Down Expand Up @@ -71,6 +74,35 @@ public function testGetFonts()
$this->assertEquals(0, \count($fonts));
}

public function testGetFontsElementMissing()
{
$headerResources = $this->getMockBuilder('Smalot\PdfParser\Header')
->disableOriginalConstructor()
->getMock();

$headerResources->expects($this->once())
->method('has')
->willReturn(true);

$headerResources->expects($this->once())
->method('get')
->willReturn(new ElementMissing());

$header = $this->getMockBuilder('Smalot\PdfParser\Header')
->disableOriginalConstructor()
->getMock();

$header->expects($this->once())
->method('get')
->willReturn($headerResources);

$page = new Page(new Document(), $header);
$fonts = $page->getFonts();

$this->assertEmpty($fonts);
$this->assertEquals([], $fonts);
}

public function testGetFont()
{
// Document with text.
Expand Down

0 comments on commit 5faf073

Please sign in to comment.