Running out of memory in tcpdi_parser
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
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
This fixed all my problems: line 940: if ((!is_array($key)) || ($key[0] == '>>')) { someone please confirm that i didn't broke something else
@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.