Reputation: 876
I am having an issue with TCPDF when attempting to render a large file: a 66 page HTML table, however the problem only happens after pushing the script to the live server.
My localhost
renders the file to completion, albeit quite slowly, but the live server returns the 500 Internal Server Error eventually.
There is nothing wrong with the actual script, as far as I can tell, as reducing the size of the HTML table down to 500 rows returns the PDF in a matter of seconds. My localhost
takes over a minute to return the same 500 lines, and 4 minutes to return the full table, so I would have thought that the live server would make short work of it but it just cannot handle it.
Likewise, the bulk content for the PDF is generated by a script that takes over 30 seconds to render on my localhost
, whereas the live server makes short work of it and can return the results in less than a second, so the performance of the live server is proven to be superior (as you'd expect, of course).
I've already set, to no avail:
ini_set("memory_limit", "-1");
ini_set('max_execution_time', 0);
set_time_limit(0);
The server is shared hosting, but I do have quite a lot of control over it and the hosting providers are willing to make reasonable changes if I can't do them myself. However, I need to pinpoint the cause before I can ask them to make any changes.
It's not ideal, but even if the file takes a long time to complete I'm happy to implement a work-around so that the file is rendered in the background and then emails the user upon completion with a download link. However, as the rendering aborts I can't even do this.
Any help is greatly appreciated!
EDIT:
I forgot to mention until reminded in the comments: I get nothing in the error logs. If I remove something critical from the script, such as the DB connector, then that logs as expected, but nothing related to TCPDF is getting logged.
Additional Info:
I was using Dompdf originally, but this was unable to render more than 100 lines of the same table without failing. This was both locally and on the live server, which is a shame because it handled CSS and HTML very well and required far fewer work-arounds for content positioning.
Upvotes: 1
Views: 2149
Reputation: 74
I had the same error and came to the conclusion that the problem was a bottleneck that forms while generating the HTML for the table and converting it to PDF format.
First I split my code into two "sections". I avoid a complex MySql query inside the loop generating the data rows by running the query externally and saving the results to a JSON file.
Then reading the JSON file I used multicell() to build my rows and columns
// Set MuliCell Defaults
$height = '5';
$border = '1';// 1/0 or L, T, R, B
$align = 'L';// L, C, R, J
$fill = '0'; //1/0
$Ln = '0';// 0=right, 1 = beginning of next line, 2=below
$floatX = '';
$floatY = '';
$resetH = 'true';
// Header
$pdf->SetFont('helvetica', 'n', 15);
$pdf->MultiCell('85', $height, 'SKU', $border, $align, $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('280', $height, 'Product', $border, $align, $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, 'Price', $border, 'R', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, 'Pk Size', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, 'Min', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, 'Ord Lvl', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, 'On Hnd', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('70', $height, 'Counted', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('32', $height, 'Var', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
// Set DataRows
$pdf->SetXY('13', '50');
$pdf->SetFont('helvetica', '', 10);
$pdf->SetTextColor(0,0,0);
$pdf->setCellPaddings(3, 3, 3, 3);
if (!empty($json))
{
$rows = '0'; // Counter for rows of processed data
foreach($json as $item) //start loop for adding
{
$pdf->Ln(18);// Set a new line BEFORE each datarow
$pdf->SetX('13');
$pdf->MultiCell('85', $height, $item['sku_prd'], $border, $align, $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('280', $height, substr($item['name_prd'],0,60), $border, $align, $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, $item['prefix_cur'].$item['sto_price'], $border, 'R', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, $item['sto_pack_size'], $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, $item['sto_min'], $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, $item['sto_reorder_level'], $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, $item['sto_qty_on_hand'], $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('70', $height, '', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('32', $height, '', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$rows++;// increment with 1 after each datarow
if ($rows == 27) // Set max number of datarows per page
{
$pdf->AddPage();
$pdf->SetXY('13', '18');
// Header
$pdf->SetFont('helvetica', 'n', 15);
$pdf->MultiCell('85', $height, 'SKU', $border, $align, $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('280', $height, 'Product', $border, $align, $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, 'Price', $border, 'R', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, 'Pk Size', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, 'Min', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, 'Ord Lvl', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, 'On Hnd', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('70', $height, 'Counted', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('32', $height, 'Var', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
// Reset font and rows to DataRows
$pdf->SetFont('helvetica', '', 10);
$pdf->SetXY('13', '25');
$rows = '0';// reset to "0" ready for the new page
}
}
}
https://www.xms-systems.co.uk/article-262-tabular-data-with-tcpdf-and-internal-server-500-error.html
Upvotes: 1