Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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/PullRequest797-pdf.js.pdf
Binary file not shown.
Binary file added samples/bugs/PullRequest797-vera.pdf
Binary file not shown.
20 changes: 12 additions & 8 deletions src/Smalot/PdfParser/RawData/RawDataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,16 @@ protected function decodeXref(string $pdfData, int $startxref, array $xref = [],
// get only the last updated version
$xref['trailer'] = [];
// parse trailer_data
if (preg_match('/Size[\s]+([0-9]+)/i', $trailer_data, $matches) > 0) {
if (preg_match('/\/Size[\s]+([0-9]+)/i', $trailer_data, $matches) > 0) {
$xref['trailer']['size'] = (int) $matches[1];
}
if (preg_match('/Root[\s]+([0-9]+)[\s]+([0-9]+)[\s]+R/i', $trailer_data, $matches) > 0) {
if (preg_match('/\/Root[\s]+([0-9]+)[\s]+([0-9]+)[\s]+R/i', $trailer_data, $matches) > 0) {
$xref['trailer']['root'] = (int) $matches[1].'_'.(int) $matches[2];
}
if (preg_match('/Encrypt[\s]+([0-9]+)[\s]+([0-9]+)[\s]+R/i', $trailer_data, $matches) > 0) {
if (preg_match('/\/Encrypt[\s]+([0-9]+)[\s]+([0-9]+)[\s]+R/i', $trailer_data, $matches) > 0) {
$xref['trailer']['encrypt'] = (int) $matches[1].'_'.(int) $matches[2];
}
if (preg_match('/Info[\s]+([0-9]+)[\s]+([0-9]+)[\s]+R/i', $trailer_data, $matches) > 0) {
if (preg_match('/\/Info[\s]+([0-9]+)[\s]+([0-9]+)[\s]+R/i', $trailer_data, $matches) > 0) {
$xref['trailer']['info'] = (int) $matches[1].'_'.(int) $matches[2];
}
if (preg_match('/ID[\s]*[\[][\s]*[<]([^>]*)[>][\s]*[<]([^>]*)[>]/i', $trailer_data, $matches) > 0) {
Expand All @@ -216,7 +216,7 @@ protected function decodeXref(string $pdfData, int $startxref, array $xref = [],
$xref['trailer']['id'][1] = $matches[2];
}
}
if (preg_match('/Prev[\s]+([0-9]+)/i', $trailer_data, $matches) > 0) {
if (preg_match('/\/Prev[\s]+([0-9]+)/i', $trailer_data, $matches) > 0) {
$offset = (int) $matches[1];
if (0 != $offset) {
// get previous xref
Expand Down Expand Up @@ -922,14 +922,18 @@ protected function getXrefData(string $pdfData, int $offset = 0, array $xref = [
throw new \Exception('Unable to find xref (PDF corrupted?)');
}

// Some files point startxref to the whitespace right before the xref keyword.
$startxrefOffset = $startxref + strspn($pdfData, $this->config->getPdfWhitespaces(), $startxref);

// check xref position
if (strpos($pdfData, 'xref', $startxref) == $startxref) {
if (strpos($pdfData, 'xref', $startxrefOffset) == $startxrefOffset) {
// Cross-Reference
$xref = $this->decodeXref($pdfData, $startxref, $xref, $visitedOffsets);
$xref = $this->decodeXref($pdfData, $startxrefOffset, $xref, $visitedOffsets);
} else {
// Check if the $pdfData might have the wrong line-endings
$pdfDataUnix = str_replace("\r\n", "\n", $pdfData);
if ($startxref < \strlen($pdfDataUnix) && strpos($pdfDataUnix, 'xref', $startxref) == $startxref) {
$startxrefUnixOffset = $startxref + strspn($pdfDataUnix, $this->config->getPdfWhitespaces(), $startxref);
if ($startxrefUnixOffset < \strlen($pdfDataUnix) && strpos($pdfDataUnix, 'xref', $startxrefUnixOffset) == $startxrefUnixOffset) {
// Return Unix-line-ending flag
$xref = ['Unix' => true];
} else {
Expand Down
18 changes: 17 additions & 1 deletion tests/PHPUnit/Integration/DocumentIssueFocusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
namespace PHPUnitTests\Integration;

use PHPUnitTests\TestCase;
use Smalot\PdfParser\Document;
use Smalot\PdfParser\Parser;

/**
Expand Down Expand Up @@ -111,4 +110,21 @@ public function testPDFDocEncodingDecode(): void
$testSubject = '•†‡…—–ƒ⁄‹›−‰„“”‘’‚™ŁŒŠŸŽıłœšž';
self::assertStringContainsString($testSubject, $details['Subject']);
}

public function testParseFileWhenStartxrefPointsToLeadingWhitespaceInVeraPdfFixture(): void
{
$document = (new Parser())->parseFile($this->rootDir.'/samples/bugs/PullRequest797-vera.pdf');

self::assertCount(1, $document->getPages());
}

/**
* @see https://github.com/smalot/pdfparser/pull/797
*/
public function testParseFileWithCompressedXrefObjectFromPdfJsCorpus(): void
{
$document = (new Parser())->parseFile($this->rootDir.'/samples/bugs/PullRequest797-pdf.js.pdf');

self::assertCount(1, $document->getPages());
}
}
Loading