smalot / smalot/pdfparser

Fatal Error when parsing some PDFs

Open
#655 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs more info
Dominant language
PHP
Stars
2.7k
Forks
579
Avg merge
2m
Merged PRs (30d)
1

Description

  • PHP Version: 7.4
  • PDFParser Version: 2.7.0 ( + 2.8.0-RC2)
Description:

Very recently started getting the following Fatal Error when trying to parse some PDF files...

PHP Fatal error: Uncaught Exception: Invalid object reference for $obj. in >../vendor/smalot/pdfparser/src/Smalot/PdfParser/RawData/RawDataParser.php:529
Stack trace:
#0 ../vendor/smalot/pdfparser/src/Smalot/PdfParser/RawData/RawDataParser.php(240): >Smalot\PdfParser\RawData\RawDataParser->getIndirectObject('%PDF-1.4\r\n%\xF9\xFA\x9A\xE7...', Array, '4', 203, true)
#1 ../vendor/smalot/pdfparser/src/Smalot/PdfParser/RawData/RawDataParser.php(905): >Smalot\PdfParser\RawData\RawDataParser->decodeXrefStream('%PDF-1.4\r\n%\xF9\xFA\x9A\xE7...', 203, Array)
#2 ../vendor/smalot/pdfparser/src/Smalot/PdfParser/RawData/RawDataParser.php(216): >Smalot\PdfParser\RawData\RawDataParser->getXrefData('%PDF-1.4\r\n%\xF9\xFA\x9A\xE7...', 203, Array)
#3 ../vendor/smalot/pdfparser/src/Smalot/PdfParser/RawData/RawDataParser.php(902): Smalot\PdfParser in >../vendor/smalot/pdfparser/src/Smalot/PdfParser/RawData/RawDataParser.php on line 529"

PDF input

I would be willing to provide a copy of the PDF if I can do so privately.

Expected output & actual output

The expected output of my code is the contents of the PDF parsed into a string of text and ultimately saved to a variable, instead there is a fatal error on certain PDF files and I really can't tell why.

Code
$ext = pathinfo($path, PATHINFO_EXTENSION);
if ( $ext == 'pdf' || $ext == 'PDF') {
    $parser = new \Smalot\PdfParser\Parser();
    $pdf = $parser->parseFile($path);
    $text = $pdf->getText();
}
return $text;

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/Smalot/PdfParser/RawData/RawDataParser.php at the reported failures around lines 529, 240, and 905. Obtain the reporter's PDF if possible, run the provided parseFile() example, and trace the xref-stream handling; done means the affected PDF parses without a fatal invalid-object-reference error and produces text.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.