Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCPDF SetFont and writeHTML

Tags:

tcpdf

I'm having a problem with TCPDF. My custom font (and any other included font) isn't working when using writeHTML.

$tcpdf = tcpdf_get_instance();
$fontname = $tcpdf->addTTFfont('/antiquariat/sites/default/files/fonts/tstar-regular-webfont.ttf', 'TrueTypeUnicode', '', 32);
$tcpdf->SetFont('tstarwebfont', '', 16);
$tcpdf->writeHTML($html);

Fonts won't change, even if I use "helvetica" or any other font. Second thing is, that the custom font isn't generated at all, but in first place I struggle with that not even any other font is used.

like image 453
Andi Avatar asked Mar 18 '23 23:03

Andi


2 Answers

This is how i did it using TCPDF:

  1. First, use this website to generate your .PHP file and .z file.
  2. Put those 2 files at the folder .../tcpdf/fonts.
  3. You will use the PHP name file one.
  4. To add your font to pdf object:
$pdf->AddFont('yourfont1');        //custom font
$pdf->AddFont('yourfont2');        //custom font

Now, to use it inside a writeHTML:

$html = '
    <style>
        h1 {
            font-family: yourfont1;
            font-size: 40pt;
            text-align:center;
        }           
    </style>
                
    <h1>Testing FONT</h1>
';

$pdf->writeHTML($html, true, false, true, false, '');
like image 106
Gabriel Avatar answered May 01 '23 10:05

Gabriel


As no one answered to me and I didn't get it up like desired, I switched to DOMPDF which is working fine so far! This is not quite the solution I was looking for, but a working one!

like image 25
Andi Avatar answered May 01 '23 11:05

Andi