TCPDF SetFont and writeHTML
Asked Answered
S

5

5

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.

Spritsail answered 22/7, 2014 at 10:26 Comment(0)
S
1

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!

Spritsail answered 29/7, 2014 at 6:52 Comment(0)
F
7

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, '');
Fundy answered 8/6, 2016 at 19:40 Comment(3)
This doesn't appear to be working. I still need to call SetFont() in order for it to work. Note that the fonts do actually embed, but CSS is not able to set the font at all.Castara
I have checked my code and i noticed i set the font after adding these two new ones, maybe you just need a default font setted even if you're not going to use it, e.g.: $pdf->SetFont('helvetica', '', 10);. Additionally, have you followed correctly each steps that i gave? If you have any issue, please post the code so i can check it.Fundy
I have already tried that. That said, TCPDF is lacking in features, and so I've switched to wkhtmltopdf.Castara
S
1

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!

Spritsail answered 29/7, 2014 at 6:52 Comment(0)
C
1

For anyone getting here by Google: To get this working I used the following code:

$josefin = TCPDF_FONTS::addTTFfont('pdf/JosefinSans-Light.ttf', 'TrueTypeUnicode', '', 96); // echo $josefin;
$pdf->AddFont('josefinsanslight');

In my CSS i used:

h2 {font-family: "josefinsanslight"; font-weight:300; color: #611c67; }

The addTTFfont-part can be skipped after using it once to generate the required files in the tcpdf font folder.

It seems the font name is derived from the file name, skipping any non alphanumerical characters and converting to lowercase.

Cinchonidine answered 17/10, 2018 at 13:37 Comment(0)
E
0

I have TCPDF 6.2.6.

I found that adding a font is a one-time operation, and should be done using the tools/tcpdf_addfont.php script. The font is converted and saved into the fonts directory, then you can refer to the font by name in your document.

Elephantiasis answered 10/3, 2016 at 5:25 Comment(0)
M
0

Don't ask TCPDF complex things. In this way work very good.

$total= <<<HTML
  <table style="width:100%;border:1px solid grey;">
    <tr>
      <td>Total </td>
      <td align="right" style="font-size:12pt;font-family: 'Times New Roman', Times, serif;"><b>$value</b></td>
    </tr>
  </table>
HTML;

$pdf->writeHTML($total, true, false, false, false, '');
Merodach answered 21/5, 2020 at 23:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.