Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent endless loops during Page->getText() because of faulty recursionStack #457

Merged
merged 14 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added samples/bugs/PullRequest457.pdf
Binary file not shown.
2 changes: 0 additions & 2 deletions src/Smalot/PdfParser/PDFObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,6 @@ public function getText(?Page $page = null): string
$result .= $text;
}

array_pop(self::$recursionStack);

return $result.' ';
}

Expand Down
10 changes: 9 additions & 1 deletion src/Smalot/PdfParser/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,15 @@ public function getText(self $page = null): string
$contents = new PDFObject($this->document, $header, $new_content, $this->config);
}

return $contents->getText($this);
/*
* Elements referecing eachother on the same page can cause endless loops during text parsing.
* To combat this we keep a recursionStack containing already parsed elements on the page.
* The stack is only emptied here after getting text from a page.
*/
$contentsText = $contents->getText($this);
PDFObject::$recursionStack = [];

return $contentsText;
}

return '';
Expand Down
32 changes: 32 additions & 0 deletions tests/Integration/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,38 @@ public function testGetText()
$this->assertStringContainsString('Verdana', $text);
}

/**
* @see https://github.com/smalot/pdfparser/pull/457
*/
public function testGetTextPullRequest457()
{
// Document with text.
$filename = $this->rootDir.'/samples/bugs/PullRequest457.pdf';
$parser = $this->getParserInstance();
$document = $parser->parseFile($filename);
$pages = $document->getPages();
$page = $pages[0];
$text = $page->getText();

$this->assertTrue(1000 < \strlen($text));
$this->assertStringContainsString('SUPER', $text);
$this->assertStringContainsString('VOORDEEL', $text);
$this->assertStringContainsString('KRANT', $text);
$this->assertStringContainsString('DINSDAG', $text);
$this->assertStringContainsString('Snelfilterkoffie', $text);
$this->assertStringContainsString('AardappelenZak', $text);
$this->assertStringContainsString('ALL', $text);

// Force garbage collection to not interfere with other tests.
unset($filename);
unset($parser);
unset($document);
unset($pages);
unset($page);
unset($text);
gc_collect_cycles();
}

public function testExtractRawData()
{
// Document with text.
Expand Down