diff --git a/src/PdfReader/Page.php b/src/PdfReader/Page.php index 33c5fe4..8cd552e 100644 --- a/src/PdfReader/Page.php +++ b/src/PdfReader/Page.php @@ -270,14 +270,24 @@ public function getContentStream() if (!($content instanceof PdfStream)) { continue; } - $result[] = $content->getUnfilteredStream(); + + try { + $result[] = $content->getUnfilteredStream(); + } catch (FilterException $e) { + // ignore streams that cannot be unfiltered + } } return \implode("\n", $result); } if ($contents instanceof PdfStream) { - return $contents->getUnfilteredStream(); + try { + return $contents->getUnfilteredStream(); + } catch (FilterException $e) { + // ignore streams that cannot be unfiltered + return ''; + } } throw new PdfReaderException( diff --git a/tests/_files/pdfs/specials/invalid_zlib_streams_issue252.pdf b/tests/_files/pdfs/specials/invalid_zlib_streams_issue252.pdf new file mode 100644 index 0000000..db0706b Binary files /dev/null and b/tests/_files/pdfs/specials/invalid_zlib_streams_issue252.pdf differ diff --git a/tests/functional/PdfReader/PageTest.php b/tests/functional/PdfReader/PageTest.php index 6ad3b51..656a9e1 100644 --- a/tests/functional/PdfReader/PageTest.php +++ b/tests/functional/PdfReader/PageTest.php @@ -148,4 +148,33 @@ public function testGetAttributeWithRecursion() $this->expectExceptionMessage('Indirect reference recursion detected (4).'); $page->getAttribute('Rotate'); } + + public function testGetContentStreamWithFaultyStreamsInContentsArray() + { + $stream = StreamReader::createByFile(__DIR__ . '/../../_files/pdfs/specials/invalid_zlib_streams_issue252.pdf'); + $parser = new PdfParser($stream); + + $pdfReader = new PdfReader($parser); + $page = $pdfReader->getPage(1); + $content = $page->getContentStream(); + $this->assertStringStartsWith( + "q\n" + . "2.8346457 0 0 2.8346457 0 0 cm q\n" + . "BT\n" + . "0 0 0 1 k\n" + . "/F0 6 Tf\n" + . "18.6606 277.3104 Td\n", + $content + ); + + $this->assertStringEndsWith( + "(Moms 25% 2793,75 \(11175,00\) ) Tj\n" + . "ET\n" + . "Q\n" + . "Q", + $content + ); + + $this->assertSame(8520, \strlen($content)); + } }