I'm getting the following error when I try and generate a PDF using the mPDF class:
TTF file "C:/wamp/www/inc/mpdf/ttfonts/verdana.ttf": invalid checksum 20f65173c11 table: DSIG (expected 65173c11)
I've uploaded the font files to my ttfonts
directory and defined the font in config_fonts.php
like this:
"verdana" => array(
'R' => "verdana.ttf",
'B' => "verdanab.ttf",
'I' => "verdanai.ttf",
'BI' => "verdanaz.ttf",
),
I only see the error when I turn on font error reporting in the config settings. When I turn error reporting off, the PDF is generated, but the font being used is not Verdana.
Any idea on what I'm doing wrong?
Based on @hrvoje-golcic answer, here's an improved and less dirty way to add fonts to mPDF without editing
config_fonts.php
. I'm using Laravel, I installed mPDF using composer._MPDF_TTFONTPATH
before initializingmPDF
with the value as the path to yourttfonts
folder (this constant exists since at least 5.3).vendor/mpdf/mpdf/ttfonts
folder to a location that you control (outside of the vendor folder).fontdata
property on themPDF
instance.Here's an example:
Composer post-install script
Instead of copying all the fonts and adding them to git, a handy workaround using a composer post-install script can do that for you.
First of all, make sure the folder where you wish to copy the fonts exists, and create a
.gitignore
in it, with the following contents:This will ignore everything except the
.gitignore
file and the fonts you wish to add.Next, add the following scripts to your
composer.json
file:Notes
This was tested to work with 6.1.
In 7.x, the author implemented an elegant way to add external fonts.
Following are the steps to add new font family in mpdf library:
newFont.ttf
font file(s) to this location/mpdf/ttfonts
folder.Edit
/mpdf/config_fonts.php
OR/mpdf/src/config/FontVariables.php
to add an entry to the$this->fontdata
array for the new font file(s). Like:font-family: 'newFont';
is now available in the stylesheets.$mpdfObj = new mPDF('', '', 'newFont'); $mpdfObj->SetFont('newFont');
Now your new font is added.
There is another "dirty" way to add fonts dynamically in the run-time aside to the lib files. This was my solution because I wasn't able to modify
config_fonts.pdf
file since it was in vendor/ files and would be overwritten on library update.For example here I'm adding 2 fonts and 3 font files because other font has regular and bold version:
This was for mpdf 5.x but hopefully it works for 6.x as well. Did anyone try?
simply add font to FontVariable.php
make sure if the ttf file name is start with capital letter like Pacifico.ttf then name the font family with start of small letter as i do on top . e.g make this pacifico and now simply test this with create testing php file
No need to show errors on the screen. See all errors and warnings in log file like "error.log" of your php + apache(?) server. It help you to find and resolve the problem based on message in the log file.
I any case you should use recommend fonts - see mPDF manual.
Probably, you need to convert TrueType fonts into proper MPDF's format. (http://mpdf1.com/manual/index.php?tid=409&searchstring=fonts)
Mpdf add Arial font
Download font file : https://github.com/JotJunior/PHP-Boleto-ZF2/blob/master/public/assets/fonts/arial.ttf
Paste the arial.ttf to mpdf/ttfonts
Open config_fonts.php and the below code with fontdata array
"arial" => array( 'R' => "arial.ttf", ),