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

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:
htdocs/pdf
|-fpdf17
| |-doc
| |-font
| |-makefont
| |-...
|-customfont
|-alienleagueii.ttf
|-alienleagueiiital.ttf
|-FREESCPT.TTF
|-JOKERMAN.TTF
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.
<?php //add new freescript font $pdf->AddFont('Freescript','','FREESCPT.php'); //add new jokerman font $pdf->AddFont('Jokerman','','JOKERMAN.php'); //add alien league (regular) $pdf->AddFont('Alien League','','alienleagueii.php');
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
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');
Use The Custom Font in PDF
To use the font, you can just call SetFont method with the family name and font variant like:$pdf->SetFont('Freescript','',36);
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.
<?php require('fpdf17/fpdf.php'); $pdf = new FPDF('P','mm','A4'); //add new freescript font $pdf->AddFont('Freescript','','FREESCPT.php'); //add new jokerman font $pdf->AddFont('Jokerman','','JOKERMAN.php'); //add alien league (regular) $pdf->AddFont('Alien League','','alienleagueii.php'); //add alien league italic $pdf->AddFont('Alien League','I','alienleagueiiital.php'); $pdf->AddPage(); //freescript font $pdf->SetFont('Freescript','',36); $pdf->Cell(190,20,'Freescript Font',0,1,'C'); //jokerman font $pdf->SetFont('Jokerman','',36); $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'); $pdf->Output();
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.
Subscribe to:
Post Comments
(
Atom
)
Sir, please create tutorial using fpdf how to insert qr code on the invoice document. Your tutorial is very nice to be watch.
ReplyDeleteSure i will. And i'll also do barcode too.
DeleteThank you.. :)
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.
ReplyDeleteAndroid 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
thanks for sharing this information
ReplyDeleteBlue Prism Training in Bangalore
Blue Prism Training in BTM
informatica Training in Bangalore
informatica Training in BTM
MERN StackTraining in Bangalore
MERN Stack Training in BTM
MEAN Stack Training in Bangalore
MEAN Stack Training in BTM
Cool you record, the information is truly salubrious further surprising, I'll give you a work nearby my scene. https://vograce.com
ReplyDelete