Skip to content

Commit 4ba8a11

Browse files
authored
Merge pull request #255 from Setasign/development
Handle filter exceptions of faulty streams in `Page::getContentStream()`. (Fixes #252)
2 parents be50a1e + 0ca4920 commit 4ba8a11

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

src/PdfReader/Page.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,24 @@ public function getContentStream()
270270
if (!($content instanceof PdfStream)) {
271271
continue;
272272
}
273-
$result[] = $content->getUnfilteredStream();
273+
274+
try {
275+
$result[] = $content->getUnfilteredStream();
276+
} catch (FilterException $e) {
277+
// ignore streams that cannot be unfiltered
278+
}
274279
}
275280

276281
return \implode("\n", $result);
277282
}
278283

279284
if ($contents instanceof PdfStream) {
280-
return $contents->getUnfilteredStream();
285+
try {
286+
return $contents->getUnfilteredStream();
287+
} catch (FilterException $e) {
288+
// ignore streams that cannot be unfiltered
289+
return '';
290+
}
281291
}
282292

283293
throw new PdfReaderException(
79.8 KB
Binary file not shown.

tests/functional/PdfReader/PageTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,33 @@ public function testGetAttributeWithRecursion()
148148
$this->expectExceptionMessage('Indirect reference recursion detected (4).');
149149
$page->getAttribute('Rotate');
150150
}
151+
152+
public function testGetContentStreamWithFaultyStreamsInContentsArray()
153+
{
154+
$stream = StreamReader::createByFile(__DIR__ . '/../../_files/pdfs/specials/invalid_zlib_streams_issue252.pdf');
155+
$parser = new PdfParser($stream);
156+
157+
$pdfReader = new PdfReader($parser);
158+
$page = $pdfReader->getPage(1);
159+
$content = $page->getContentStream();
160+
$this->assertStringStartsWith(
161+
"q\n"
162+
. "2.8346457 0 0 2.8346457 0 0 cm q\n"
163+
. "BT\n"
164+
. "0 0 0 1 k\n"
165+
. "/F0 6 Tf\n"
166+
. "18.6606 277.3104 Td\n",
167+
$content
168+
);
169+
170+
$this->assertStringEndsWith(
171+
"(Moms 25% 2793,75 \(11175,00\) ) Tj\n"
172+
. "ET\n"
173+
. "Q\n"
174+
. "Q",
175+
$content
176+
);
177+
178+
$this->assertSame(8520, \strlen($content));
179+
}
151180
}

0 commit comments

Comments
 (0)