Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a new font to a fpdf?

Tags:

add

fonts

fpdf

How can I add the Gotham-book font to fpdf. Most explanations are unclear and very difficult to understand. I need an example. Please can someone assist?

like image 638
Janinekey Avatar asked Sep 17 '14 10:09

Janinekey


People also ask

How do I add fonts to Fpdf?

It is necessary to generate a font definition file first with the MakeFont utility. The definition file (and the font file itself when embedding) must be present in the font directory. If it is not found, the error "Could not include font definition file" is raised.

How do I add a custom font to Tcpdf?

// convert TTF font to TCPDF format and store it on the fonts folder $fontname = TCPDF_FONTS::addTTFfont('/path-to-font/FreeSerifItalic. ttf', 'TrueTypeUnicode', '', 96); // use the font $pdf->SetFont($fontname, '', 14, '', false); Save this answer.


2 Answers

Nevermind Got my answer

Go to http://www.fpdf.org/makefont/

Convert your .ttf file

Download the .php and the .z file

Add these files to the following folder: fpdf/font
(This is the default folder structure that you need to download in order for your fpdf to work)

This is the code I have used in my file:

$fpdf->AddFont('Gotham','','Gotham-Book.php');
    $fpdf->SetFont('Gotham','',11); 
like image 89
Janinekey Avatar answered Oct 24 '22 05:10

Janinekey


I was also stuck with this error, @Janinekey's answer is right but I didn't find how to add variants of a font. This article explains this process really well too.

I will explain the whole process in steps.

Step 1. Download all the variant's of your font (Regular, Bold, Italic, Bold-Italic)

Step 2. Convert all the fonts to font-name.php and font-name.z from this link

Step 3. Copy all *.php and *.z files in fonts/ folder which is in root directory of fpdf.php. [You can rename *.php files, but don't rename *.z once you converted them from ttf].

Step 4. Use this code to import the font into your pdf:

//Importing Font and it's variations
$fpdf->AddFont('Gotham','','Gotham-Book.php'); //Regular
$fpdf->AddFont('Gotham','B','Gotham-Book-bold.php'); //Bold
$fpdf->AddFont('Gotham','I','Gotham-Book-italic.php'); //Italic
$fpdf->AddFont('Gotham','BI','Gotham-Book-bold-italic.php'); //Bold_Italic

//Now use it as normal font
$fpdf->SetFont('Gotham','B',11);

PS: Font name will be same for different variants. So, make sure you don't change it, otherwise you will treat it as another font completely.

like image 9
Sumit Badsara Avatar answered Oct 24 '22 06:10

Sumit Badsara