Skip to content

Commit 7c00709

Browse files
authored
Handle falsy header elements, fixes #514 (#525)
1 parent 193515a commit 7c00709

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/Smalot/PdfParser/Header.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ public function has(string $name): bool
132132
*/
133133
public function get(string $name)
134134
{
135-
if (\array_key_exists($name, $this->elements)) {
136-
return $this->resolveXRef($name);
135+
if (\array_key_exists($name, $this->elements) && $element = $this->resolveXRef($name)) {
136+
return $element;
137137
}
138138

139139
return new ElementMissing();

src/Smalot/PdfParser/PDFObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function __construct(
7878
?Config $config = null
7979
) {
8080
$this->document = $document;
81-
$this->header = null !== $header ? $header : new Header();
81+
$this->header = $header ?? new Header();
8282
$this->content = $content;
8383
$this->config = $config;
8484
}

tests/Integration/HeaderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ public function testGet(): void
155155
$this->assertTrue($header->get('Font') instanceof Page);
156156
$this->assertTrue($header->get('Image') instanceof ElementMissing);
157157
$this->assertTrue($header->get('Resources') instanceof ElementMissing);
158+
159+
/**
160+
* A double forward slash in the header's content results in a falsy element
161+
* that should be parsed to ElementMissing instead.
162+
*
163+
* @see https://github.com/smalot/pdfparser/pull/525
164+
*/
165+
$content = '<</Type/Page/SubType//Text>>foo';
166+
$position = 0;
167+
$header = Header::parse($content, $document, $position);
168+
169+
$this->assertTrue($header->get('SubType') instanceof ElementMissing);
158170
}
159171

160172
public function testResolveXRef(): void

0 commit comments

Comments
 (0)