Programming, tutorials, mechatronics, operating systems, and other tech stuff

Create, Add, and Use Custom Font in PHP FPDF Library

By default, fonts which can be used to generate pdf file using PHP FPDF Library are only predefined standard fonts bundled along FPDF Library such as Courier, Helvetica, Arial, Times, Symbol, and ZapfDingbats. Contrary to apps like MS Word which could use fonts installed in the computer, FPDF won't recognize any fonts installed on the computer.

FPDF could not directly use fonts from ttf files. instead, the ttf file should be converted into fpdf font library which consists of php files and a "z" file. We need to convert the ttf font into those library using a script provided by fpdf library in order to be able to use the fonts in our documents.
In this tutorial, i'll explain how to convert, add and use custom font in FPDF library.
For how to use fpdf library to generate pdf file in pdf, see Generate Printable Invoice in PHP using FPDF Library.

Create The FPDF Font Library by Converting TTF Files

To turn ttf font into php in order to use it in our generated pdf document, we can use makefont.php script inside makefont directory provided along with fpdf library distribution. You can download fpdf library here.
First, extract the files into your web directory (htdocs or www). Inside, you'll find a folder named "makefont".
Next, create a directory to place your ttf files (ie. customfont) which will be converted into fpdf font library.
This is how my directories looks like:
  |   |-doc
  |   |-font
  |   |-makefont
  |   |-...
I took 4 fonts as an example in this tutorial.
Run your command line or terminal, then enter the customfont directory using cd command.
Then run this command:
php <path to makefont.php>\makefont.php <font file name>.ttf

Where <path to makefont.php> is either static or relative path to your makefont.php file, and the <font file name> is your font file name followed by .ttf extension. It's case sensitive.
You'll get something like this.
Repeat those steps for other fonts.

Add The Custom Fonts in FPDF Library

After you done converting the ttf files, go back to your customfont directory, you'll get some php and z file with font names in it.
Copy those files, leaving aside the ttf files, into the font directory inside fpdf17 directory.
You'll get something like this.
Then create a new php file for the pdf generator, include the fpdf.php library, and create a new FPDF object (Read Generate Printable Invoice in PHP using FPDF Library for further references on how to do that).

To add the font into the documents, use AddFont method of fpdf library.

//add new freescript font

//add new jokerman font

//add alien league (regular)
$pdf->AddFont('Alien League','','alienleagueii.php');
The first parameter is family name, second parameter is the font variant, third parameter is font's php file name.

The font family name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
The font variant can be either of these strings:
  • empty string: regular
  • B: bold
  • I: italic
  • BI or IB: bold italic
The default value is regular.

For example, in this tutorial, i have 2 forms of Alien League fonts, alienleagueii.php for regular font, and alienleagueiiital.php for the italic font. I need to add both font separately with same family name but different variant and font files. So it should looks like this:

//add alien league (regular)
$pdf->AddFont('Alien League','','alienleagueii.php');

//add alien league italic
$pdf->AddFont('Alien League','I','alienleagueiiital.php');
Please keep in mind that you need to add the fonts before you use it. So it's usually placed after fpdf object definition.

Use The Custom Font in PDF

To use the font, you can just call SetFont method with the family name and font variant like:

Keep in mind that Freescript font used in this tutorial have no other variants beside regulars. So $pdf->SetFont('Freescript','I',36); would not work.  But $pdf->SetFont('Alien League','I',36); will because we add the italic variants of Alien League font beforehand.

Here's the complete scripts.


$pdf = new FPDF('P','mm','A4');

//add new freescript font

//add new jokerman font

//add alien league (regular)
$pdf->AddFont('Alien League','','alienleagueii.php');

//add alien league italic
$pdf->AddFont('Alien League','I','alienleagueiiital.php');


//freescript font
$pdf->Cell(190,20,'Freescript Font',0,1,'C');

//jokerman font
$pdf->Cell(190,20,'Jokerman Font',0,1,'C');

//alien league regular font
$pdf->SetFont('Alien League','',36);
$pdf->Cell(190,20,'Alien League Regular Font',0,1,'C');

//alien league italic font
$pdf->SetFont('Alien League','I',36);
$pdf->Cell(190,20,'Alien League Italic Font',0,1,'C');


And here's what the results look like.

For the video explanation of this tutorial, please watch this video. And for other tutorials regarding PHP FPDF Library, please watch my youtube PHP FPDF Tutorial Series.


  1. Sir, please create tutorial using fpdf how to insert qr code on the invoice document. Your tutorial is very nice to be watch.

    1. Sure i will. And i'll also do barcode too.
      Thank you.. :)

  2. Hello, I read your blog occasionally, and I own a similar one, and I was just wondering if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me insane, so any assistance is very much appreciated.
    Android Course Training in Chennai | Best Android Training in Chennai
    Selenium Course Training in Chennai | Best Selenium Training in chennai
    Devops Course Training in Chennai | Best Devops Training in Chennai

  3. Cool you record, the information is truly salubrious further surprising, I'll give you a work nearby my scene.