Recently, I am using TCPDF for my report function, but I found the PDF generating process is really slow, about 5 mins!
I checked my code and the library souce code, and found the Font Type is main problem. I also found there is a post in Joomla forum. http://forum.joomla.org/viewtopic.php?p=1499840
in tcpdf.php replace:
$font = $this->_getTrueTypeFontSubset($font, $subsetchars);
with this code:
// 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);
}




Thanks for this!
My printout went from 30secs a page to less than a second per page after this modification.
Have you tried setting setFontSubsetting(false) for your TCPDF-Object? This does basically the same but does not need any patching on the TCPDF-Files. I experienced a similar speed bost like Richard.
Amazing!
From 2 minutes to 2 seconds!!!!
Thank you. You are a genius.
hi,
Here, Vietnames font is erros
This is great. On my local machine it was taking about 10 seconds but on the development server (a Pentium 4 2.0GHz with 512MB of RAM) was causing PHP to time out and if I allowed it to run till the end it would take nearly two minutes.
With this fix it’s now instantaneos!!
hey guys..
i also tested the setfontsubsetting(false) method but it’s useless. the files generated then are huge. (1 mb w/o pictures)
i’m still searching for another solution, right now i use dompdf wich is pretty but not stable.
OMG, you are genius! thanks for that
Notice that this caches all of the characters you use on the first PDF it generates. This is a GREAT timesaver, but make sure that your first page you generate has all of these characters! I recommend using a large HTML metacharacter page as your test page.
Actually – this one just uses a subset that are the 0-512 utf-8 characters. For anyone using a greek letter (math, science, etc.) change that line to not specify the list, and then run this once with the a page like http://htmlhelp.com/reference/html40/entities/symbols.html will give you a set that should do for ANY page you make!
GENIUS! TAHNK YOU VERY MUCH!!!
You’re just awesome !
From 30 seconds to 2 seconds !
You are a legend!!!!
For correct vietnames chars add
// Latin Extended Additional (256: 1E00–1EFF)
for ($i = 7680; $i <= 7936; $i++) {
$subsetchars[$i] = true;
}
Perfect! Thanks a lot! That helped me very much!
I’ve tried to add this fix for an arabic translation I’m doing in TCPDF using the Almohanad font.
It seems to ignoring the code? Has anyone tested this with the aforementioned font?
this code is certainly very helpful as i did commented the line with $font and replaced with the code mentioned & Woila…. magic, but the very first time it takes 5 sec’s. and then after that it loads nearly in a sec. this is cool.
That is very, very, very helpful.
Thank you!
Well, unfornutately it doesn’t seem to work with arabic text. my arabic text disappeared from the pdf after this modification…
This patch is no more required with the latest TCPDF version because the subsetting performances and bugs were fixed.
I have a problem with some (not all) UTF-8 characters for Slovene language. I have created new instance of TCPDF like that:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, ‘UTF-8′, false);
I am experiencing problems with letters: Č and Ć. Letters like Š and Ž are ok.
I have also tried to do iconv( ‘UTF-8′, ‘cp1250′, “Č Š Ž Ć” ); with no luck.
How can I solve this problem?
Mod for cyrillic chars.
after:
$subsetchars = array_fill(0, 512, true);
add:
// Cyrillic Chars (0410–044F)
for($i=1040;$i<=1103;$i++){
$subsetchars[$i] = true;
}
Hi,
Can you please provide me with the Hebrew Mod chars.
Thanks in Advance
Orly