Skip to content

Commit

Permalink
Check for binary content in formatContent() before a problematic rege…
Browse files Browse the repository at this point in the history
…xp (#676)

* Move formatContent check for binary content

Some PDF files manage to serve binary content (a font, or an image, etc.) to the formatContent() method which only accepts text document streams.

Since (strings) in PDF document streams can contain binary content (that gets decoded by a font library, for instance) we must test the document stream for text-only content EXCLUDING (string) content.

Previously the check for text-only content was done *after* a regexp removing the (string)s from the stream, recursively extending the regexp to check for balanced parentheses. However for a sufficiently long binary stream, this might create a regexp long enough to cause a PHP error.

The solution is to move the check for binary content *before* this (string)-removing regexp. Simplify the binary check by truncating the document stream at the first open parenthesis, which indicates the start of a (string), then testing what remains for valid UTF-8.

This makes the later check for binary content unnecessary and it has been removed.

Future work on this issue should be done to determine why PdfParser is creating PDFObject objects from binary data in the first place.

* Update PDFObject.php

Avoid using a throwaway variable.

* Update PDFObject.php

Don't update this indentation.

* Update src/Smalot/PdfParser/PDFObject.php

Use strict false check.

Co-authored-by: Konrad Abicht <[email protected]>

---------

Co-authored-by: Konrad Abicht <[email protected]>
  • Loading branch information
GreyWyvern and k00ni authored Feb 26, 2024
1 parent 9a16bae commit 2939dfa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
Binary file added samples/bugs/Issue668.pdf
Binary file not shown.
21 changes: 10 additions & 11 deletions src/Smalot/PdfParser/PDFObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ private function formatContent(?string $content): string
return '';
}

// Outside of (String) content in PDF document streams, all
// text should conform to UTF-8. Test for binary content by
// deleting everything after the first open-parenthesis ( which
// indicates the beginning of a string. Then test what remains
// for valid UTF-8. If it's not UTF-8, return an empty string
// as this $content is most likely binary.
if (false === mb_check_encoding(preg_replace('/\(.*$/s', '', $content), 'UTF-8')) {
return '';
}

// Find all strings () and replace them so they aren't affected
// by the next steps
$pdfstrings = [];
Expand Down Expand Up @@ -261,17 +271,6 @@ private function formatContent(?string $content): string
);
}

// Now that all strings and dictionaries are hidden, the only
// PDF commands left should all be plain text.
// Detect text encoding of the current string to prevent reading
// content streams that are images, etc. This prevents PHP
// error messages when JPEG content is sent to this function
// by the sample file '12249.pdf' from:
// https://github.com/smalot/pdfparser/issues/458
if (false === mb_detect_encoding($content, null, true)) {
return '';
}

// Normalize white-space in the document stream
$content = preg_replace('/\s{2,}/', ' ', $content);

Expand Down
10 changes: 10 additions & 0 deletions tests/PHPUnit/Integration/PDFObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,16 @@ public function testFormatContent(): void
$cleaned = $formatContent->invoke($this->getPdfObjectInstance(new Document()), $content);

$this->assertEquals('', $cleaned);

// See: https://github.com/smalot/pdfparser/issues/668
$filename = $this->rootDir.'/samples/bugs/Issue668.pdf';

$parser = $this->getParserInstance();
$document = $parser->parseFile($filename);
$pages = $document->getPages();

// Binary check is done before a regexp that causes an error
$this->assertStringContainsString('Marko Nestorović PR', $pages[0]->getText());
}

public function testGetSectionsText(): void
Expand Down

0 comments on commit 2939dfa

Please sign in to comment.