Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set different Fonts in TCPDF

Tags:

php

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.

like image 631
TJR Avatar asked Jun 05 '12 12:06

TJR


People also ask

How can I download TCPDF library?

Go to their github page https://github.com/tecnickcom/tcpdf either download to desktop or git clone.

Could not include font definition?

You may see this error when generating your PDF catalog: TCPDF ERROR: Could not include font definition file: arialmt. The error means that the font (in this example arialmt) is missing in the modules configuration files. You will therefore need to add the font in the corresponding folder.


2 Answers

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);
like image 136
Andi Avatar answered Sep 21 '22 17:09

Andi


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');
like image 36
Developer Avatar answered Sep 19 '22 17:09

Developer