From 4d6864a70bc61068a240a72a4198588728b2cefc Mon Sep 17 00:00:00 2001 From: PrinsFrank <25006490+PrinsFrank@users.noreply.github.com> Date: Mon, 18 Oct 2021 07:32:02 +0200 Subject: [PATCH] Fix TypeError on default font when no fonts available (#466) * 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 --- src/Smalot/PdfParser/PDFObject.php | 5 ++++- tests/Unit/PDFObjectTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/PDFObjectTest.php diff --git a/src/Smalot/PdfParser/PDFObject.php b/src/Smalot/PdfParser/PDFObject.php index 7dd36d5a..2bce9ce6 100644 --- a/src/Smalot/PdfParser/PDFObject.php +++ b/src/Smalot/PdfParser/PDFObject.php @@ -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); diff --git a/tests/Unit/PDFObjectTest.php b/tests/Unit/PDFObjectTest.php new file mode 100644 index 00000000..593f3ec9 --- /dev/null +++ b/tests/Unit/PDFObjectTest.php @@ -0,0 +1,25 @@ +getText()); + } + + public function testGetTextOnPageWithoutContent(): void + { + $document = new Document(); + + static::assertSame(' ', (new PDFObject($document, null, null))->getText(new Page($document))); + } +}