TJR
TJR

Reputation: 6577

How to set different Fonts in TCPDF

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.

Upvotes: 9

Views: 64720

Answers (6)

powtac
powtac

Reputation: 41040

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.

Upvotes: 3

th3penguinwhisperer
th3penguinwhisperer

Reputation: 466

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 :)

Upvotes: 1

Vaibhavi Azrekar
Vaibhavi Azrekar

Reputation: 13

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.

Upvotes: -2

Developer
Developer

Reputation: 3998

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');

Upvotes: 2

Andi
Andi

Reputation: 311

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);

Upvotes: 13

TJR
TJR

Reputation: 6577

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.

Upvotes: -2

Related Questions