How to add custom fonts to TCPDF?

I would like to add my own font to the pdf that I generate using TCPDF. Maybe something is missing, but the documents seem to be out of date. They refer to the addTTFfont() function, but I think it is deprecated and no longer exists in the latest version of TCPDF.

I read that I need to convert the ttf file and put it in the fonts folder so that I run:

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

and he generated these files, which are now in the fonts folder:

 rumpelstiltskinwebfont.ctg.z rumpelstiltskinwebfont.z rumpelstiltskinwebfont.php 

Then I tried to add a font:

 $pdf->addFont('rumpelstiltskin'); $pdf->SetFont('rumpelstiltskin', '', 14, '', false); 

but I get an error:

 TCPDF ERROR: Could not include font definition file: rumpelstiltskin 
+6
source share
2 answers

I understood my problem, I was almost there.

Here is a step by step:

First, convert the font using the font of the tcpdf_addfont.php tool to the TCPDF tool folder:

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

This will create the necessary files and place them in the TCPDF font folder. Check the font folder and copy the font name, in my case it was rumpelstiltskinwebfont .

In your code, install the font using the font file name and write a line of text:

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

What is it. Hope this helps someone. :)

+9
source

Got this answer in another question and decided for me. You just need to use the first parameter, the path to the font file. Works with TTF and OTF fonts.

It generates a name string for use with $pdf->SetFont($fontname, '', $font_size);

Hope this helps.


The latest version of TCPDF automatically converts fonts to TCPDF using the addTTFfont() method. For instance:

 // 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 more information and examples, please see the TCPDF Fonts documentation page.

NOTE. After the font has been converted, TCPDF no longer requires a TTF file or the above addTTFfont() call!

0
source

Source: https://habr.com/ru/post/986057/


All Articles