Php – TCPDF – How to make the printing faster? It is very very slow, 1320 records took 40 minutes

PHPtcpdfwindowszend-dbzend-framework

Using Zend framework and TCPDF this is taking 40 minutes to print the Test.pdf. I having now no idea how to reduce this abnormal time to something normal?

set_time_limit(0);
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setLanguageArray($l);
$pdf->setFontSubsetting(true);
$pdf->SetFont('dejavusans', '', 8, '', true);
$pdf->AddPage();
/* Database mysql gives the records and it is wrapped with <table> */
$html = "<table>1310 records.... with some simple <tr><td></td></tr></table>";
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
$pdf->Output('Test.pdf', 'I');
exit;

Follow up: (tune the performance)

1) php.ini:
memory_limit = 512M
max_execution_time = 0

2) Codeing
$pdf->setFontSubsetting(false); // true to false

3) Debug shows, following taking the whole time

$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);

Best Answer

Since no other answers have been forthcoming, I would highly recommend a good look at MPDF (GPL) as it's much faster than TCPDF. I've had operations on one server that took approx three minutes using TCPDF being reduced to seconds with MPDF. I would only assume that some format of my HTML -> PDF was hitting some inefficient function in TCPDF.

Anyway I present the following code which helped me convert HTML -> PDF.

$mpdf = new mPDF('c');
$mpdf->setDisplayMode('fullpage');
$stylesheet = file_get_contents('css/core.css');
$mpdf->WriteHTML($stylesheet,1);
$html = "<table>1310 records.... with some simple <tr><td></td></tr></table>";
$mpdf->WriteHTML($html); 
$mpdf->Output(standardize(ampersand('filename', false)) . '.pdf', 'D');

This code provides a PDF outputted as a downloadable file, the MPDF documentation gives lots of other examples to suit your needs.

Related Topic