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

Running out of memory in tcpdi_parser #29

Open
Marchiuzzz opened this issue Aug 4, 2021 · 3 comments
Open

Running out of memory in tcpdi_parser #29

Marchiuzzz opened this issue Aug 4, 2021 · 3 comments

Comments

@Marchiuzzz
Copy link

Marchiuzzz commented Aug 4, 2021

I've been using this package for over a year now and suddenly started getting memory exhaustion errors
PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 20480 bytes) in <[...]>/tcpdi_parser.php on line 882
Here's my code

public function downloadFiles($files, $name = 'name.pdf'){
      $pdf = new \TCPDI();
      $pdf->SetPrintHeader(false);
      $pdf->SetPrintFooter(false);
      $filesArr = [];
      $pdf->SetTitle($name);
      foreach($files as $f){
        $filePath = $f->getFirstMedia('pdf')->getFullUrl();
        $width = $f->sheetsize->width;
        $height = $f->sheetsize->height;
        $port_land = ($width > $height) ? "landscape" : "portrait";
        $pdf->AddPage($port_land, array($width, $height));

  	$pageCount = $pdf->setSourceFile($filePath);
        for($i = 1; $i <= $pageCount; $i+=1)
        {
          $tplId = $pdf->importPage($i);
          $pdf->useTemplate($tplId, 0, 0, $width);
          if($pageCount > $i){
            $pdf->AddPage();
          }
        }

      }
      $name = str_replace('.pdf', '', $name).'.pdf';
      $pdf->Output($name, "I");
    }

I'm attaching one of the pdfs that causes this error, I'm really lost at where should I even begin to look for the problem.
problems.pdf

@noxtras
Copy link

noxtras commented Feb 6, 2025

I have the same error +
Trying to access array offset on value of type int [tcpdi_parser.php:940]

I will pay for a fix.

Im on PHP 8.1
// File name : tcpdi_parser.php
// Version : 1.1

@noxtras
Copy link

noxtras commented Feb 6, 2025

This fixed all my problems:
line 940: if ((!is_array($key)) || ($key[0] == '>>')) {
someone please confirm that i didn't broke something else

@Marchiuzzz
Copy link
Author

@noxtras I never actually found a fix but I did figure out a workaround. In my case, this issue doesn't happen every time but with specific PDFs, so I assume they have something corrupt in them that TCPDI can't process. So the solution was to integrate optional PDF parsing by GhostScript, so we pre-process the PDF with GS and then pass this new PDF to the tcpdi, which seems to work 99.99% of the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants