smalot / smalot/pdfparser

ErrorException Undefined array key 38

Open
#647 5 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: 8.1
  • PDFParser Version: "smalot/pdfparser": "2.7.0"
Description:

I'm getting an error when trying to run getDataTm() on a pdf. I have tried this on a native LAMP stack and inside a Symfony 6.3.6 framework.
ErrorException - Undefined array key 38

PDF input

420399_20231024.pdf

Expected output & actual output
Code
    $parser = new Parser();
    $pdf = $parser->parseFile($uploadedFile);
    $totalPages = $pdf->getDetails()['Pages'];
    for ($p = 0; $p < $totalPages; $p++) {
          **$data[$p] = $pdf->getPages()[$p]->getDataTm();   //  <---- error happen here**
    }


I temporary solved wrapping the read of array "$extractedTexts" in a "array_key_exists()" checking and all pdf texts seems correctly retrivied except for last text in the bottom right:"Page 1 of n" that is lost.

In "vendor/smalot/pdfparser/src/Smalot/PdfParser/Page.php":

Before fix:

704	$currentText = $extractedTexts[\count($extractedData)];
705         switch ($command['o']) {

After fix:

704     if (array_key_exists(\count($extractedData), $extractedTexts)) {
705         $currentText = $extractedTexts[\count($extractedData)];
706         switch ($command['o']) {
              . . .
878         }
879	}

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/Page.php around line 704, then reproduce getDataTm() with the attached PDF and the reported PHP 8.1 setup. Trace how extractedTexts and extractedData are indexed during parsing. Done means the PDF parses without an undefined array key and the bottom-right “Page 1 of n” text is not lost.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.