Hi. I'm a bit confused about handling fonts with TCPDF. I'm trying to make use of font subsetting to squeeze the minimum possible file size out of the library. Most of my text is in Times New Roman with a little bit in Verve. I've downloaded the Verve.ttf file and converted it to TCPDF files using the CLI script. My code looks something like:
$this->lib = new \TCPDF();
$this->lib->setFontSubsetting(true);
$this->lib->AddPage();
$this->lib->SetFont('Verve', '', 40);
Passing either true of false for setFontSubsetting() will result in the same file size. Same with the subsetting argument for SetFont(). I have verified that the font is getting embedded, but it seems it is either being or not being subset regardless of whether I've enabled or disabled subsetting. What am I doing wrong?
Also, I understand that Times New Roman will be ignored for subsetting because it is a core TCPDF font. Is that correct?
Thanks.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've done some more digging in PDF readers and this is what I've found out:
With subsetting on false, fonts show as Embedded
With subsetting on true, fonts show as Embedded Subset.
File size is exactly the same.
Font file extracted from the PDF file contains all the characters from the original font file even with subsetting on.
I can only conclude that TCPDF is trying to do the subsetting but embeds all the characters anyway for some reason.
Any workaround for this problem?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've looked at TCPDF code and identified the following obstacles to getting my fonts properly subset:
Found "First 256 chars are now always included on font subset to overcome a problem reported on the forum." in the changelog. This is a problem because I am only using the characters from the first 256 and I don't want the rest embedded. The fix is implemented on line 4408 as
$subsetchars = array_fill(0, 255, true);
Setting the fill value to 0 or false doesn't work because TCPDF seems to refuse to alter an element as long as it is defined. I've managed to get proper arrays of font's I'm using in my test by initializing this array as empty.
$subsetchars = array();
Another problem is that TCPDF_FONTS::_getTrueTypeFontSubset($font, $subsetchars) simply returns $font for all my fonts. The method actually returns $font early in line 949. Adding some debugging code to TCPDF and TCPDF_FONTS classes, I managed to get the following output:
verve.z sfnt version must be 0x00010000 for TrueType version 1.0, found 0x4f54544f
arialb.z sfnt version must be 0x00010000 for TrueType version 1.0, found 0x4f54544f
arial.z sfnt version must be 0x00010000 for TrueType version 1.0, found 0x4f54544f
I have no idea what this means. I used http://fonts.snm-portal.com/ to generate my TCPDF font files. Is that bad? Should I use the CLI script instead or should I somehow fix my TTF files first?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm enjoying your conversation. I've not tried to do this myself but I know tcpdf does come with command line tools for generating the fonts and this is the recommended way.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The first 256 characters are always embedded.
Please check the tools directory inside the TCPDF folder for bulk font conversion or follow the default examples.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi. I'm a bit confused about handling fonts with TCPDF. I'm trying to make use of font subsetting to squeeze the minimum possible file size out of the library. Most of my text is in Times New Roman with a little bit in Verve. I've downloaded the Verve.ttf file and converted it to TCPDF files using the CLI script. My code looks something like:
Passing either true of false for setFontSubsetting() will result in the same file size. Same with the subsetting argument for SetFont(). I have verified that the font is getting embedded, but it seems it is either being or not being subset regardless of whether I've enabled or disabled subsetting. What am I doing wrong?
Also, I understand that Times New Roman will be ignored for subsetting because it is a core TCPDF font. Is that correct?
Thanks.
I've done some more digging in PDF readers and this is what I've found out:
With subsetting on false, fonts show as Embedded
With subsetting on true, fonts show as Embedded Subset.
File size is exactly the same.
Font file extracted from the PDF file contains all the characters from the original font file even with subsetting on.
I can only conclude that TCPDF is trying to do the subsetting but embeds all the characters anyway for some reason.
Any workaround for this problem?
I've looked at TCPDF code and identified the following obstacles to getting my fonts properly subset:
Setting the fill value to 0 or false doesn't work because TCPDF seems to refuse to alter an element as long as it is defined. I've managed to get proper arrays of font's I'm using in my test by initializing this array as empty.
verve.z sfnt version must be 0x00010000 for TrueType version 1.0, found 0x4f54544f
arialb.z sfnt version must be 0x00010000 for TrueType version 1.0, found 0x4f54544f
arial.z sfnt version must be 0x00010000 for TrueType version 1.0, found 0x4f54544f
I have no idea what this means. I used http://fonts.snm-portal.com/ to generate my TCPDF font files. Is that bad? Should I use the CLI script instead or should I somehow fix my TTF files first?
Yes, http://fonts.snm-portal.com/ is not working properly. It's generating font files without a loca table.
Sorry for talking to myself to you :)
I'm enjoying your conversation. I've not tried to do this myself but I know tcpdf does come with command line tools for generating the fonts and this is the recommended way.
The first 256 characters are always embedded.
Please check the tools directory inside the TCPDF folder for bulk font conversion or follow the default examples.