Skip to content

Commit 2083625

Browse files
committed
Optimized support for ignoring faulty structures in page tree
The parsing of the page tree is stopped now as soon as the page count is reached. By doing this we can ignore following faulty objects in the Kids structure.
1 parent 7781f58 commit 2083625

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

composer.lock

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/PdfReader/PdfReader.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ protected function readPages($readAll = false)
202202
return;
203203
}
204204

205-
$readPages = function ($kids, $count) use (&$readPages, $readAll) {
205+
$expectedPageCount = $this->getPageCount();
206+
$readPages = function ($kids, $count) use (&$readPages, $readAll, $expectedPageCount) {
206207
$kids = PdfArray::ensure($kids);
207208
$isLeaf = ($count->value === \count($kids->value));
208209

@@ -222,6 +223,11 @@ protected function readPages($readAll = false)
222223
} else {
223224
$this->pages[] = $object;
224225
}
226+
227+
// stop if all pages are read - faulty documents exists with additional entries with invalid data.
228+
if (count($this->pages) === $expectedPageCount) {
229+
break;
230+
}
225231
}
226232
};
227233

463 Bytes
Binary file not shown.

tests/functional/PdfReader/PdfReaderTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public function getPageCountProvider()
4949
13
5050
];
5151

52+
$data[] = [
53+
$path . '/specials/page-trees/PageTreeWithInvalidTypeAndMisslLeadingCount.pdf',
54+
1
55+
];
56+
5257
return $data;
5358
}
5459

@@ -835,6 +840,27 @@ public function getPageProvider()
835840
]
836841
];
837842

843+
$data[] = [
844+
$path . '/specials/page-trees/PageTreeWithInvalidTypeAndMisslLeadingCount.pdf',
845+
[
846+
1 => PdfIndirectObject::create(
847+
1,
848+
0,
849+
PdfDictionary::create([
850+
'Type' => PdfName::create('Page'),
851+
'MediaBox' => PdfArray::create([
852+
PdfNumeric::create(0),
853+
PdfNumeric::create(0),
854+
PdfNumeric::create(595.28),
855+
PdfNumeric::create(841.89)
856+
]),
857+
'Resources' => PdfDictionary::create(),
858+
'Parent' => PdfIndirectObjectReference::create(3, 0),
859+
])
860+
)
861+
]
862+
];
863+
838864
return $data;
839865
}
840866

0 commit comments

Comments
 (0)