DEV Community

ekastrya
ekastrya

Posted on

How to Add Fonts to TCPDF in Your Project

(Versi Bahasa Indonesia: https://medium.com/@ekastrya/cara-menambah-font-untuk-tcpdf-17af6808b76)

Earlier today, I was assigned a simple task to add two custom fonts for a client's project. It took me 15 minutes on Stack Overflow to find a fast way to do this. Found it and here are the steps:

  1. Install / download / require the tcpdf library into your project.
  2. Open the library folder (for example mine was: ./vendor/tecnickcom/tcpdf).
  3. Notice and enter to the tools folder.
  4. Copy the TTF file(s) into this tools folder.
  5. Run this command from the terminal (still from the tools folder): $ php tcpdf_addfont -i BOOKOS.TTF (Replace BOOKOS.TTF with your own fonts)
  6. Repeat step (5) for each fonts desired.

Coding time..
To change between fonts from your php code, use the following scripts.

$pdf->SetFont($font_family = ‘bookos’, $variant = ‘’, $fontsize = 11);

(Versi Bahasa Indonesia: https://medium.com/@ekastrya/cara-menambah-font-untuk-tcpdf-17af6808b76)

Top comments (3)

Collapse
 
hasintha profile image
Hasintha Weragala

I'm getting some gibberish characters, please help

Collapse
 
aamirmadari profile image
Aamir Madari

Works perfectly! Thanks

Collapse
 
kalel profile image
Khalil Djahli • Edited

Somehow I had to add '.php' to 'tcpdf_addfont' to make it work.
Should we delete the fonts from the 'tools' folder after we execute the command?