Skip to content

Commit

Permalink
Fix TypeError on default font when no fonts available (#466)
Browse files Browse the repository at this point in the history
* Add test for scenarios where either the page is null or there is no content, both failing on a return type for 'getDefaultFont'

* Don't add the font to the set of available fonts when there is no font available

* Run csfixer

Co-authored-by: prinsfrank <[email protected]>
  • Loading branch information
PrinsFrank and prinsfrank authored Oct 18, 2021
1 parent f5fa4d3 commit 4d6864a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Smalot/PdfParser/PDFObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ private function getDefaultFont(Page $page = null): Font
$fonts = $page->getFonts();
}

$fonts[] = $this->document->getFirstFont();
$firstFont = $this->document->getFirstFont();
if (null !== $firstFont) {
$fonts[] = $firstFont;
}

if (\count($fonts) > 0) {
return reset($fonts);
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/PDFObjectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Tests\Smalot\PdfParser\Unit;

use Smalot\PdfParser\Document;
use Smalot\PdfParser\Page;
use Smalot\PdfParser\PDFObject;
use Tests\Smalot\PdfParser\TestCase;

class PDFObjectTest extends TestCase
{
public function testGetTextOnNullPage(): void
{
static::assertSame(' ', (new PDFObject(new Document()))->getText());
}

public function testGetTextOnPageWithoutContent(): void
{
$document = new Document();

static::assertSame(' ', (new PDFObject($document, null, null))->getText(new Page($document)));
}
}

0 comments on commit 4d6864a

Please sign in to comment.