Karol
Karol

Reputation: 93

TCPDF optimize performance

I've done a lot of searching and testing. My task is to optimize PDF generation form HTML.

My code is:

<?php
define('K_TCPDF_EXTERNAL_CONFIG', true); 

require('static_config.php');
require("hipero_pdf.class.php");

$pdf = new hipero_TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);

$pdf->SetHeaderData(PDF_HEADER_IMAGE, PDF_HEADER_IMAGE_WIDTH);
$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->SetFont('dejavusans', '', 10);

$pdf->AddPage();

$pdf->SetTextColor(0, 0, 0);

$pdf->writeHTML(PDF_SYNEO_HTML, true, false, true, false, '');
$pdf->Output('../../Content/Offers/Generated/'.PDF_SYNEO_FILENAME.'.pdf');
?>

My HTML is a bit messy, but test shows this is have almost no impact on performance.

Every peace of code take less than 1 sek. to be parsed. Only this take longer:

$pdf->AddPage - 3 sek.

$pdf->writeHTML - 9 sek.

$pdf->Output - 5 sek.

This is on IIS server (shared hosting).

Please tell me how to optimize this methods. I have no more ideas :/.

Upvotes: 3

Views: 7911

Answers (2)

Vic Seedoubleyew
Vic Seedoubleyew

Reputation: 10536

I also had an issue with TCPDF performance. I followed the guidelines cited at http://www.tcpdf.org/performances.php. What I could take from it :

  • writeHTML() made a big difference instead of cell(), but only for already short times. It divided by 5 the time taken for pieces that didn't take more than 5 seconds. But above that, it didn't make much of a difference.
  • on the contrary, cutting big pieces in small worked really well for me. I tried to cut big pieces into smaller ones, and it seemed that the optimal was around 5000 characters per piece. It enabled me to bring a pdf generation from 60 seconds to 20 seconds, and 7 seconds on my server.

Hope it helps !

Upvotes: 2

Daniel Silva
Daniel Silva

Reputation: 39

At TCPDF website, there's a page which advises avoiding the use of writeHTML if possible. Other than that, it also advises avoiding use big HTML blocks. There's more at http://www.tcpdf.org/performances.php where they explain you can turn off additional options to make rendering faster.

Upvotes: 3

Related Questions