8

I am looking for a solution to set more than just one font for my PDF document which is created with tcpdf.

I want to do something like this:

$pdf->SetFont('verdana_bold', 'B', 12);
$pdf->SetFont('verdana', '', 12);

I need for my document a bold font and a regular font. The example above doesn't work. When I switch the two lines the text is all bold. When I use the example above the text is just regular.

I want to set the font-weight with regular css stylesheets.

Hope you have a solution.

franzlorenzon
  • 5,845
  • 6
  • 36
  • 58
TJR
  • 6,307
  • 10
  • 38
  • 64
  • 2
    Alternatively, use PhantomJS and have that produce a PDF using WebKit rendering. http://phantomjs.org/ – starlocke Jun 05 '12 at 14:16
  • http://stackoverflow.com/questions/5263588/how-to-implement-custom-fonts-in-tcpdf – Alfred Mar 31 '14 at 18:28
  • If running PhantomJS from Linux systems, it still requires Freetype and Fontconfig. http://phantomjs.org/download.html – JD. Nov 15 '17 at 14:25

6 Answers6

11

You can use your custom fonts inside html like this:

$fontname=$pdf->addTTFfont('path/myfont.ttf', '', '', 32);
//echo $fontname;

$html='<span style="font-family:'.$fontname'.;font-weight:bold">my text in bold</span>: my normal text';
$pdf->writeHTMLCell($w=0,$h=0,$x=11,$y=201,$html,$border=0,$ln=0,$fill=false,$reseth=true,$align='L',$autopadding=false);
Andi
  • 191
  • 2
  • 5
2

Convert Verdana for TCPDF usage:

$fontname = $pdf->addTTFfont('/path-to-font/verdana.ttf', 'TrueTypeUnicode', '', 32);
  • Make sure the fonts folder is writeable
  • Have you set the path K_PATH_FONTS constant to the fonts in your config/tcpdf_config.php ?
  • Read trough TCPDF Fonts.
indrarajw
  • 198
  • 2
  • 18
powtac
  • 40,542
  • 28
  • 115
  • 170
  • 1
    When i upload my verdana.ttf to my webserver its automaticly splittet in 4 files. Verdana.ttf, Verdanai.ttf (italic), verdanab.ttf(bold) and verdanaib.ttr(bold italic) - but in ur way i just add one font - for example verdana.ttf - but there is no solution to get the bold and the regular font. – TJR Jun 05 '12 at 13:35
2

The lines below will generate 3 files in your fonts folder

rotisserifi56.php, rotisserifi56.ctg, rotisserifi56.rar

use this to generate the file

$fontname = $this->pdf->addTTFfont('fonts/Rotis Serif Italic 56.ttf', 'TrueTypeUnicode', '', 32);


// use the font
$this->pdf->SetFont($fontname, '', 14, '', false);

Now, use the fonts like this:

$this->pdf->AddFont('rotisserifi56', '', 'rotisserifi56.php');
$this->pdf->SetFont('rotisserifi56');
Lohardt
  • 1,057
  • 1
  • 12
  • 26
Developer
  • 3,857
  • 4
  • 37
  • 47
1

I know this question is pretty old but I had the same problem and fixed it.

What you CAN do but SHOULDN'T do is:

$pdf->SetFont('verdana_bold', 'B', 12);
$pdf->SetFont('verdana', '', 12);

What you basically do here is define 2 fonts. One with the name verdana and one with name verdana_bold. While you specify the B for bold it can't find this ttf. Because TCPDF basically checks for a file in the fonts dir called verdana_boldb.ttf. This doesn't exist so it takes the verdana_bold.ttf (which at first sight seems to be the correct behavior).

For me the issue got noticable after I tried to use both bold and non-bold styles in a table and I either only got the whole table in bold or the whole table in non-bold (removing or adding the B style specifier doesn't make a difference).

What you SHOULD do:

Add the new font type:

$fontname = TCPDF_FONTS::addTTFfont($fontfile, 'TrueType', '', 32);

When you want to use the font:

$pdf->SetFont('verdana', '', 10, '', false);

When you want items in bold in a HTML cell use the html b tag:

<b>myvalue</b>

You can check in the fonts directory if you have the verdanab.ttf file.

$ ls fonts/verdanab.
verdanab.ctg.z  verdanab.php    verdanab.z

I hope this helps someone else :)

-2

I just fixed my problem. The problem was, that the fonts must be named in the right way. verdana_bold is wrong - it must be verdanab. Then i just have to register the verdana font and tcpdf grab automaticly the verdanab.ttf for the bold version of this font.

TJR
  • 6,307
  • 10
  • 38
  • 64
-2

steps to inlcude custom font:

  1. you can find the .ttf file of the custom font you want from c://windows/fonts directory.
  2. copy that file into the fonts folder located at tcpdf/fonts
  3. use $pdf->addTTFfont('C://wamp/www/projectname/...path to .ttf file', 'TrueTypeUnicode', '', 32);
  4. $pdf->SetFont('custom_font_name');

your custom font is ready to use.

Smern
  • 18,746
  • 21
  • 72
  • 90
  • You are not addressing the issue. The issue is that bold and italic wont work unless you follow a naming convention. – Lohardt Nov 15 '13 at 14:55