How to add TTF font to html2pdf PHP program
Asked Answered
E

4

10

I'm trying to use HTML2PDF 4.03 with this code:

<?php
$content = "..."; # my HTML code
require_once(dirname(__FILE__).'/html2pdf_v4.03/html2pdf.class.php');
$html2pdf = new HTML2PDF('P','A4','en', true, 'utf-8', array(15,20,15,20) );
# here I'm trying to add my arial.ttf
$html2pdf->pdf->AddTTFFont('arial.ttf');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');
?>

Now the program die with this:

PHP Fatal error:  Call to undefined method HTML2PDF_myPdf::AddTTFFont()

How can I add TTF font to my PDF file?

Empurple answered 25/1, 2015 at 16:55 Comment(0)
L
14

I have managed to add 1 custom font to my setup using the following method.

First convert the .ttf file to 3 separate files (.php .z and .ufm) using the following font converter Place the 3 files that are generated by this system into the fonts folder in TCPDF.

Now you can set the default font for your PDF using the following command

$html2pdf->setDefaultFont("the_name_you_called_your_font");

This was fairly simple to get working, I am having issues using 2 seperate fonts though via this method. I'll figure it out though

Laudian answered 14/4, 2015 at 2:57 Comment(0)
L
3

To expand on the selected answer (by o11y_75) when you convert your fonts, you need to use a specific name to include also the bold and italic variants. That way, you only add one font definition like this

$html2pdf->AddFont('opensans', 'normal', 'opensans.php');
$html2pdf->setDefaultFont('opensans');

When you convert the fonts, name them, for example, like these:

default: opensans
bold: opensansb
italic: opensansi
bold italic: opensansbi

notice that behind the original name, you add b, i and bi on each case. I have found no documentation on this issue, but I followed the nomenclature found on the fonts that already came with TCPDF and it worked.

Lawhorn answered 22/5, 2016 at 18:40 Comment(0)
U
1

If you want to add multiple fonts, just use :

$html2pdf->addFont('opensansregular', '', 'opensansregular');
$html2pdf->addFont('opensansbold', '', 'opensansbold');

I would suggest that you don't use special chars with the font converter specified above.

Then in your CSS simply type :

<style type="text/css">
<!--
.uppercase {
    text-transform: uppercase;
}
* {
    font-family: opensansregular;
}
h1, h2, h3, strong {
    font-family: opensansbold;
}
-->
</style>
Undoubted answered 26/4, 2016 at 7:59 Comment(2)
I tried it but it does not work. I have two font in my tcpdf/fonts folder. Both work when I add them as default. Let's say: $html2pdf->setDefaultFont("francoisone");. But when I try this: $html2pdf->setDefaultFont('OpenSans'); $html2pdf->addFont('francoisone', '', 'francoisone');, I can't use font-family: francoisone; in my css. Is there something wrong with my setup?Brat
Yes, I convertet FrancoisOne.ttfto francoisone.afm, francoisone.php, francoisone.z. And it works as DefaultFont. But not as addFont, when called via CSS h1, h2, h3, h4 {font-family: francoisone;}Brat
W
0

HTML2PDF works internally with TCPDF. TCPDF has its own object since version 6.2.6 to create the fonts needed for HTML2PDF: TCPDF_FONTS

I have solved this as follows: I searched for TCPDF in the vendor directory and found the Fonts directory there. Then I created my own separate PDF script and used it once to create the necessary font files from the ttf.

usage: $fontname = TCPDF_FONTS::addTTFfont('vendor/tecnickcom/tcpdf/fonts/arialuni/arialuni.ttf', 'TrueTypeUnicode', '', 32);

For more details see: https://mcmap.net/q/915166/-how-do-you-add-custom-fonts-in-tcpdf

Whiffen answered 13/12, 2021 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.