Skip to content

Commit e2e3581

Browse files
EnThibEnzok00ni
authored
fix: allow to parse null or empty header element (#560)
* fix: allow to parse null or empty header element * fix: change return null by ElementNull * feat: add unit test for issue557 pdf file * fix: make empty array for empty headers * fix: PHP CS files fix * fix: remove empty() to replace it by raw comparison Co-authored-by: Konrad Abicht <[email protected]> * removed obsolete ) Co-authored-by: Enzo <[email protected]> Co-authored-by: Konrad Abicht <[email protected]>
1 parent 13a8178 commit e2e3581

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

samples/bugs/Issue557.pdf

23.3 KB
Binary file not shown.

src/Smalot/PdfParser/Parser.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ protected function parseHeader(array $structure, ?Document $document): Header
262262
*/
263263
protected function parseHeaderElement(?string $type, $value, ?Document $document)
264264
{
265+
$valueIsEmpty = null == $value || '' == $value || false == $value;
266+
if (('<<' === $type || '>>' === $type) && $valueIsEmpty) {
267+
$value = [];
268+
}
269+
265270
switch ($type) {
266271
case '<<':
267272
case '>>':

tests/Integration/ParserTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,30 @@ public function testIssue391(): void
270270
);
271271
}
272272

273+
/**
274+
* Tests if a PDF with null or empty string headers trigger an Exception.
275+
*
276+
* It happened because there was a check missing in Parser.php (parseHeaderElement function).
277+
*
278+
* @see https://github.com/smalot/pdfparser/issues/557
279+
*/
280+
public function testIssue557(): void
281+
{
282+
/**
283+
* PDF provided by @DogLoc for usage in our test environment.
284+
*
285+
* @see https://github.com/smalot/pdfparser/pull/560#issue-1461437944
286+
*/
287+
$filename = $this->rootDir.'/samples/bugs/Issue557.pdf';
288+
289+
$document = $this->fixture->parseFile($filename);
290+
291+
$this->assertStringContainsString(
292+
'Metal Face Inductive Sensor',
293+
$document->getText()
294+
);
295+
}
296+
273297
/**
274298
* Tests behavior when changing default font space limit (-50).
275299
*

0 commit comments

Comments
 (0)