Skip to content

Commit

Permalink
fix: allow to parse null or empty header element (#560)
Browse files Browse the repository at this point in the history
* 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 <hi@inspirito.de>

* removed obsolete )

Co-authored-by: Enzo <e.thibert@gda.fr>
Co-authored-by: Konrad Abicht <hi@inspirito.de>
  • Loading branch information
3 people committed Dec 5, 2022
1 parent 13a8178 commit e2e3581
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
Binary file added samples/bugs/Issue557.pdf
Binary file not shown.
5 changes: 5 additions & 0 deletions src/Smalot/PdfParser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ protected function parseHeader(array $structure, ?Document $document): Header
*/
protected function parseHeaderElement(?string $type, $value, ?Document $document)
{
$valueIsEmpty = null == $value || '' == $value || false == $value;
if (('<<' === $type || '>>' === $type) && $valueIsEmpty) {
$value = [];
}

switch ($type) {
case '<<':
case '>>':
Expand Down
24 changes: 24 additions & 0 deletions tests/Integration/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,30 @@ public function testIssue391(): void
);
}

/**
* Tests if a PDF with null or empty string headers trigger an Exception.
*
* It happened because there was a check missing in Parser.php (parseHeaderElement function).
*
* @see https://github.com/smalot/pdfparser/issues/557
*/
public function testIssue557(): void
{
/**
* PDF provided by @DogLoc for usage in our test environment.
*
* @see https://github.com/smalot/pdfparser/pull/560#issue-1461437944
*/
$filename = $this->rootDir.'/samples/bugs/Issue557.pdf';

$document = $this->fixture->parseFile($filename);

$this->assertStringContainsString(
'Metal Face Inductive Sensor',
$document->getText()
);
}

/**
* Tests behavior when changing default font space limit (-50).
*
Expand Down

0 comments on commit e2e3581

Please sign in to comment.