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

Check for wrong line-endings when getting xref #635

Merged
merged 2 commits into from
Aug 24, 2023
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
17 changes: 15 additions & 2 deletions src/Smalot/PdfParser/RawData/RawDataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -901,8 +901,15 @@ protected function getXrefData(string $pdfData, int $offset = 0, array $xref = [
// Cross-Reference
$xref = $this->decodeXref($pdfData, $startxref, $xref);
} else {
// Cross-Reference Stream
$xref = $this->decodeXrefStream($pdfData, $startxref, $xref);
// 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) {
// Return Unix-line-ending flag
$xref = ['Unix' => true];
} else {
// Cross-Reference Stream
$xref = $this->decodeXrefStream($pdfData, $startxref, $xref);
}
}
if (empty($xref)) {
throw new \Exception('Unable to find xref');
Expand Down Expand Up @@ -937,6 +944,12 @@ public function parseData(string $data): array
// get xref and trailer data
$xref = $this->getXrefData($pdfData);

// If we found Unix line-endings
if (isset($xref['Unix'])) {
$pdfData = str_replace("\r\n", "\n", $pdfData);
$xref = $this->getXrefData($pdfData);
}

// parse all document objects
$objects = [];
foreach ($xref['xref'] as $obj => $offset) {
Expand Down
3 changes: 0 additions & 3 deletions tests/PHPUnit/Integration/FontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,6 @@ public function testDecodeUnicode(): void
$this->assertEquals('AB', Font::decodeUnicode("\xFE\xFF\x00A\x00B"));
}

/**
* @group linux-only
*/
public function testDecodeText(): void
{
$filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf';
Expand Down