Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCPDF change font inside html

I converted the .ttf font i need (3 files), and included them inside the tcpdf fonts folder.

It works just fine when i use the classic $pdf->SetFont(); call, but the problem is i dont know how to change font inside the html which is later called using $pdf->writeHTML();

Let us call it the font X.

For example:

$pdf->SetFont('DejaVu Sans','',10);

$html = '
<table>
<tr>
<td><span style="font-family:X;"></span></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
';

$pdf->writeHTML($html, true, false, true, false, '');

What i want, is to use the dejavu sans font through all the document, except for the <span> inside the first <td> tag, where i need the font X. I tried using the font-family property but it does not work so far.

like image 231
Biker John Avatar asked Jul 09 '26 08:07

Biker John


1 Answers

As of 2019, the accepted answer is not correct anymore. In short — it works nowadays!

I've tested the following example in TCPDF 6.2.9:

$pdf->AddFont('PTSans', '', 'ptsans.php');
$pdf->AddFont('DejaVuSans', '', 'dejavusans.php')

/* Setting the font family: */
$pdf->setFont('PTSans', '', 9);

/* outputting with writeHTMLCell, which internally calls MultiCell(), 
 * which in turn uses writeHTML() for HTML 
 */
$pdf->writeHTMLCell(100, 10, '', '', '<span style="font-family:DejaVuSans;">А</span>A (<span style="font-family:DejaVuSans;">Ö</span>Ö)')

I see the following (screenshot)

enter image description here

Note that I've tried writing the font family name as DejaVuSans, dejavusans, Deja Vu Sans, 'Deja Vu Sans' (with quotes) — all of these work, provided the font has the needed characters. I suppose that TCPDF lowercases the font family and strips whitespace from it.

like image 65
Matvey Andreyev Avatar answered Jul 10 '26 23:07

Matvey Andreyev