From 54559901ad37baf4e47b502a5648892d43cac24d Mon Sep 17 00:00:00 2001 From: Brian Huisman Date: Tue, 26 Sep 2023 11:42:18 -0400 Subject: [PATCH] Return empty array if no valid command detected In some edge cases, the formatContent() method may return a document stream row containing an invalid command. Make sure we just ignore these commands instead of triggering warnings for missing $matches array elements. --- src/Smalot/PdfParser/PDFObject.php | 5 +++++ tests/PHPUnit/Integration/PDFObjectTest.php | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Smalot/PdfParser/PDFObject.php b/src/Smalot/PdfParser/PDFObject.php index 96ab2592..60f45b40 100644 --- a/src/Smalot/PdfParser/PDFObject.php +++ b/src/Smalot/PdfParser/PDFObject.php @@ -974,6 +974,11 @@ public function getCommandsText(string $text_part, int &$offset = 0): array preg_match('/^(([\/\[\(<])?.*)(?assertEquals('Tf', $fontCommandHyphen[0]['o']); $this->assertEquals('FID-01 15.00', $fontCommandHyphen[0]['c']); } + + /** + * Tests that an invalid command does not cause an error, but just + * returns an empty array + */ + public function testInvalidCommand(): void + { + $pdfObject = $this->getPdfObjectInstance(new Document()); + + $validCommand = $pdfObject->getCommandsText('75 rg'); + + $this->assertEquals('', $validCommand[0]['t']); + $this->assertEquals('rg', $validCommand[0]['o']); + $this->assertEquals('75', $validCommand[0]['c']); + + $invalidCommand = $pdfObject->getCommandsText('75'); + + $this->assertEquals([], $invalidCommand); + } }