Cannot add font with Html2pdf / TCPDF (PHP)

Viewed 1091

Using Html2pdf PHP lib ("spipu/html2pdf": "^5.2") I can not add a new font.

I've tried with "DancingScript" (downloaded from google font).

Here is my code:

$html2pdf = new Html2Pdf('P', 'A4', 'fr', true, 'UTF-8', array(0, 0, 0, 0));
$html2pdf->addFont('DancingScript', 'regular', '/var/.../font/DancingScript/DancingScript-Regular.ttf');

It says:

500 Internal Server Error - Error details: [0] TCPDF ERROR: The font definition file has a bad format: /var/.../www/font/DancingScript/DancingScript-Regular.ttf in /var/.../www/vendor/tecnickcom/tcpdf/tcpdf.php on line 2950. Backtrace: #0 /var/.../www/vendor/tecnickcom/tcpdf/tcpdf.php(4302): TCPDF->Error() #1 /var/.../www/vendor/spipu/html2pdf/src/Html2Pdf.php(464): TCPDF->AddFont() 
2 Answers

After many hours, I finally figured out how to do...

First, download the ttl files of your font. For example, I've downloaded the free font "DancingScript-Regular" from Google font.

Then, go to https://www.xml-convert.com/fr/convertir-fichiers-polices-ttf-en-afm-pfa-fpdf-tcpdf to convert your file into php. I choosed "DancingScript-Regular.ttl" and it created "DancingScript-Regular.php" in my example.

Place the converted files and the font files into the same folder.

Then, here is what it should look like:

$html2pdf = new Html2Pdf('P', 'A4', 'fr', true, 'UTF-8', array(0, 0, 0, 0));
$html2pdf->addFont('DancingScript', 'regular', '/var/.../font/DancingScript/DancingScript-Regular.php');

Then in you CSS block:

div.foo {
    font-family: DancingScript;
    font-size: 14pt;
}
Related