How to Add Custom Fonts in Tcpdf

How to implement custom fonts in TCPDF

The latest TCPDF version automatically convert fonts into TCPDF format using the addTTFfont() method. For example:

// convert TTF font to TCPDF format and store it on the fonts folder
$fontname = TCPDF_FONTS::addTTFfont('/path-to-font/FreeSerifItalic.ttf', 'TrueTypeUnicode', '', 96);

// use the font
$pdf->SetFont($fontname, '', 14, '', false);

For further information and examples, please check the TCPDF Fonts documentation page.

NOTE: Once the font has been converted, TCPDF no longer requires the TTF file or the above call to addTTFfont()!

How do you add custom fonts in TCPDF?

I figured out my issue, I was almost there.

Here is a step by step:

First convert your font using the tcpdf_addfont.php tool font in the TCPDF tools folder:

php/tcpdf/tools/tcpdf_addfont.php -i php/font/rumpelstiltskin-webfont.ttf

This will generate the required files and put them in the TCPDF fonts folder. Check the fonts folder and copy the name of the font, in my case it was rumpelstiltskinwebfont.

In your code set the font using the font file's name and write a line of text:

$pdf->SetFont('rumpelstiltskinwebfont', '', 14, '', false);
$pdf->Write(0, 'Fill text', '', 0, '', true, 0, false, false, 0);

That's it. Hope this helps someone. :)

Adding new fonts in TCPDF

You can do the below thing. If you have .ttf file for the new font then convert that font to .php and .z files using below link
http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf

Once you converted the fonts download the .php and .z file and upload it into tcpdf's fonts folder. TCPDF will consider your filename as your fontname so make sure you have correct file names.

Once files are uploaded you can directly use setfont function of tcpdf.

Custom Font installation in Laravel TCPDF

Try placing your font in the tcpdf/fonts folder or setting the K_PATH_FONTS constant to the location of your font. Either way, it is unnecessary (and probably unwise) to keep your font folder in the public folder.

tcpdf custom font is not being used

Got it to work by using this tool

http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf

it generates a .php, .z, and .afm file that you put in the TCPDF fonts directory. I'm still not sure what the problem was. the addTTFfont() method doesn't create the .afm file so maybe that's it?



Related Topics



Leave a reply



Submit