TCPDF twice as slow as FPDF with same code

泪湿孤枕 提交于 2019-11-30 05:41:48

Here is a sweet solution, shaves 2 minutes for me. PDFs are created in 3 seconds!

http://www.bitrealm.net/2010/08/tcpdf-is-slow-here-is-the-solution/

Replace

$font = $this->_getTrueTypeFontSubset($font, $subsetchars);

with this:

/ Alcal: $font2cache modification
// This modification creates utf-8 fonts only the first time,
// after that it uses cache file which dramatically reduces execution time
if (!file_exists($fontfile.'.cached')){
// calculate $font first time
$subsetchars = array_fill(0, 512, true); // fill subset for all chars 0-512
$font = $this->_getTrueTypeFontSubset($font, $subsetchars); // this part is actually slow!
// and then save $font to file for further use
$fp=fopen($fontfile.'.cached','w');
$flat_array = serialize($font); //
fwrite($fp,$flat_array);
fclose($fp);
}
else {
// cache file exist, load file
$fp=fopen($fontfile.'.cached','r');
$flat_array = fread($fp,filesize($fontfile.'.cached'));
fclose($fp);
$font = unserialize($flat_array);
}

http://www.tcpdf.org/performances.php

By default TCPDF enables font subsetting to reduce the size of embedded Unicode TTF fonts, this process, that is very slow and requires a lot of memory, can be turned off using setFontSubsetting(false) method;

This was the real solution for me.

Since version 5.9.067 TCPDF performances were drastically improved. Each new release seems performing better. Additionally you can set it to boost performances as explained at http://www.tcpdf.org/performances.php

TCPDF performances can be tuned by disabling unused features on the configuration file and turn off slow features like font subsetting. Using only core fonts (like Helvetica, Times, ...) in non-UTF8 mode you can get good performances. Additionally you can install XCache on you server to boost PHP performances. Check the official http://www.tcpdf.org website and forums for further information.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!