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

13,223

Solution 1

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.

Solution 2

One of the causes of the TCPDF slow performance could be images inserted from external URLs. DNS resolution and file downloading take time and slow down the PDF generation process.

Solution 3

We use mpdf mainly because I can just "include" it with no installation. There was some minor tweak that needed to be added to php.ini, mbstring I think.

I was able to get 75 pages a minute up to 100 pages per minute by shrinking the HTML I was feeding the WriteHTML verb. We do not have any graphics.

Then I use ghostscript to sort and cat pdfs (15 or so pages) per person receiving them. That cat process takes about 3 minutes per 800-1000-pages for the 50 or 60 people receiving a report at one destination.

All this on a box mostly sitting there for nothing else.

Share:
13,223

Related videos on Youtube

Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin over 1 year

    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);

    • Peter Brooks
      Peter Brooks almost 12 years
      In my experience I've found MPDF (mpdf1.com/mpdf) much faster compared to TCPDF. Beware that TCPDF is LGPL whereas MPDF is GPL.
  • Admin
    Admin almost 12 years
    I could not reduce the time with TCPDF, it is a hell even not using WriteHTML simple Cell. MPDF made this fastest.
  • user2166860
    user2166860 over 10 years
    Thanks for the suggestion on switching from TCPDF to MPDF. Doing so reduced the PDF creation time on a certain doc from 240 seconds to 7 seconds.