1

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 2

2

With html2pdf you can not use ttf Files. You have to convert your ttf files to php files

for example here: https://www.edoardovignati.it/how-to-install-custom-fonts-in-html2pdf/

Then add the converted php Files in the Folder TCPDF-master/fonts

Now you should be able to use the font inside Html2Pdf.

Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.