August 10, 2019 14

How to use custom font in PDF | PHP FPDF Tutorial #5

How to use custom font in PDF | PHP FPDF Tutorial #5


welcome to the fourth (fifth ^_^) chapter of PHP FPDF tutorial series. in this short chapter, i’m about to demonstrate how to add custom font in our PDF document. and we will also learn how to deal with font variants. as an examples, we will use three different font families. Jokerman, Freescript, and Alien League second edition. Alien League, however, will have two variants which is regular version, and italic version. so here we have 2 font files for Alien League, one is regular the other is italic. in FPDF, we cannot use the font’s TTF file directly to add a new font. so in FPDF directory, there is a script called makefont.php. we use makefont to convert ttf file into .z file and .php file which stores characters definition, so let’s do this. to make things easier, first, copy the directory path of your makefont.php file. then you need to put your fonts file in a directory. in my example, customfont directory. open command prompt, use cd command to move into your fonts directory. then execute makefont.php with the font file name as parameter. a .php and .z file with the same name will be generated. These are the files you need. do it again for each fonts. close your command prompt, copy those files into “font” directory inside “fpdf17” directory. then let’s make the PDF generator. to define a new font, we need to use AddFont() method with three parameters. first parameter is family name. the name can be chosen arbitrarily. just to make it straight, i’ll simply use the font names. second is style or variant. the default value is regular. third is the php file of the respective fonts we’ve just made. let’s add each font. because Alien League will have two variants, we need to define it twice. both should have the same family name, but different variant and php file. then finally use SetFont() method to use the fonts. i’ve made a mistake here by using wrong php file name in font definition. let’s fix it. this concludes this chapter of tutorial. thank you for watching. see you next time.

14 Replies to “How to use custom font in PDF | PHP FPDF Tutorial #5”

  • chamindu yasitha says:

    that's great

  • Tajih Mokhtar says:

    Thank you

  • Osama Isam says:

    How to setfont utf-8 arabic

  • HMONG GLOBAL VIDEO CHANEL says:

    How to set Lao font(Phesarath OT.ttf) help please

  • Keerthinaik R says:

    the best easy tutorial can u let us know about how to add background color and background image to the pdf

  • Rana Brar says:

    How add mangal font on tcpdf

  • Lav Saden says:

    Warning: Character Uacute is missing
    Warning: Character Ucircumflex is missing
    Warning: Character Udieresis is missing
    Warning: Character Yacute is missing
    Warning: Character Thorn is missing
    Warning: Character germandbls is missing
    Warning: Character agrave is missing
    Warning: Character aacute is missing
    Warning: Character acircumflex is missing
    Warning: Character atilde is missing
    Warning: Character adieresis is missing
    Warning: Character aring is missing
    Warning: Character ae is missing
    Warning: Character ccedilla is missing
    Warning: Character egrave is missing
    Warning: Character eacute is missing
    Warning: Character ecircumflex is missing
    Warning: Character edieresis is missing
    Warning: Character igrave is missing
    Warning: Character iacute is missing
    Warning: Character icircumflex is missing
    Warning: Character idieresis is missing
    Warning: Character eth is missing
    Warning: Character ntilde is missing
    Warning: Character ograve is missing
    Warning: Character oacute is missing
    Warning: Character ocircumflex is missing
    Warning: Character otilde is missing
    Warning: Character odieresis is missing
    Warning: Character divide is missing
    Warning: Character oslash is missing
    Warning: Character ugrave is missing
    Warning: Character uacute is missing
    Warning: Character ucircumflex is missing
    Warning: Character udieresis is missing
    Warning: Character yacute is missing
    Warning: Character thorn is missing
    Warning: Character ydieresis is missing

    how to fix ?

  • Lav Saden says:

    Warning: Character Eth is missing how to change it

  • enrico lemuel says:

    Thank you!

  • codeego says:

    keren kali tutorialnya … cool … thanks very much

  • Shirish Patil says:

    hello Madam, I used your tutorial for generating pdf..thank you for such a good tutorial.
    I also followed for the custom font….our local language is Hindi…when i tried converting font file shivaji05.ttf to shivaji05.php i am not getting the font reflected in the pdf. It shows the default font. Please help me…

  • Jaime Vera Sobino says:

    thanx
    🙂

  • Bruno Cordeiro says:

    nice bro! you save me.

  • ariyansgroup says:

    Hi, Iam getting the following error.

    Error : Fatal error: Uncaught exception 'Exception' with message 'FPDF error: Undefined font: serifa bt B' in /home3/giyae9a/public_html/mydomain.com/latest/fpdf.php:271 Stack trace: #0 /home3/giyae9a/public_html/mydomain.com/latest/fpdf.php(510): FPDF->Error('Undefined font:…') #1 /home3/giyae9a/public_html/mydomain.com/latest/print_pdf.php(96): FPDF->SetFont('Serifa BT', 'B', 9) #2 {main} thrown in /home3/giyae9a/public_html/mydomain.com/latest/fpdf.php on line 271

    what to do? shall I need to add font directory? please help. I already done serifa bt .php & .z conversion and placed at folder "Font"

Leave a Reply

Your email address will not be published. Required fields are marked *

© Copyright 2019. Amrab Angladeshi. Designed by Space-Themes.com.