How to implement custom fonts in TCPDF

109,656

Solution 1

The latest TCPDF version automatically convert fonts into TCPDF format using the addTTFfont() method. For example:

// 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);

For further information and examples, please check the TCPDF Fonts documentation page.

NOTE: Once the font has been converted, TCPDF no longer requires the TTF file or the above call to addTTFfont()!

Solution 2

I have discovered a very good tool online. The only thing you need to do is to upload your .ttf file and then download the files and copy then into the /fonts folder.

https://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf

Solution 3

The addTTFfont method is not available on TCPDF main class so following worked for me.

// convert TTF font to TCPDF format and store it on the fonts folder
$fontname = TCPDF_FONTS::addTTFfont('pathto/arial.ttf', 'TrueTypeUnicode', '', 96);

// use the font
$pdf->SetFont($fontname, '', 14, '', false);

Hope this helps!

Solution 4

the below lines will generate 3 files in ur fonts folder 1.rotisserifi56.php 2.rotisserifi56.ctg 3.rotisserifi56.rar

    use this to generate the required php and other files
$fontname = $this->pdf->addTTFfont('D:/wamp/www/projectname/sites/all/modules/civicrm/packages/tcpdf/fonts/Rotis Serif Italic 56.ttf', 'TrueTypeUnicode', '', 32);

    // use the font
    $this->pdf->SetFont($fontname, '', 14, '', false);

Now,

use the fonts like this:

 $this->pdf->AddFont('rotisserifi56', '', 'rotisserifi56.php');
$this->pdf->SetFont('rotisserifi56');

--hope this helps some one :)

Solution 5

First create .php ,.afm,.z from http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf and move all three in same directory containing .ttf file. then Use This :

$pdf->AddFont(path-to/universe.ttf','',path-to/universe.php');
$pdf->SetFont(path-to/universe.ttf','',10);
Share:
109,656

Related videos on Youtube

Alfred
Author by

Alfred

I am a Full Stack developer and a DevOps Engineer, who always to learn from, and contribute to, the technology community. I was a beginner in programming once. What all I had was pieces of scattered and crude knowledge (don't know if i can call it knowledge) then. Later, I joined for Master of Computer Applications in a college and there, I got a great teacher. It was her, who taught me when and where to use 'while' and 'for' loops even.What all knowledge I have now, and what all achievements I've made till now, is only because of her. Compared to her, I am ashes. Hats off my dear teacher Sonia Abraham, you are the best of your kind. Sonia Abraham is a professor of the Department of Computer Applications, M.A College of Engineering, Mahatma Gandhi University

Updated on July 09, 2022

Comments

  • Alfred
    Alfred almost 2 years

    In TCPDF, there are only a few fonts to choose from, to create pdf files. I want to set Tahoma as my pdf font. How can I include Tahoma in TCPDF??

  • lol
    lol over 11 years
    how come this works sometimes but returns empty font definition files the other times?
  • JamesG
    JamesG over 10 years
  • Joe Murray
    Joe Murray over 9 years
    Heh @Developer, it looks like you are using CiviCRM. We're trying to launch a new StackExchange site for CiviCRM at ttp://area51.stackexchange.com/proposals/77367/civicrm -- we have lots of people interested, but we need another 36 people with 200+ reputation on another StackExchange site to commit before we can launch. Would you help us out? Thanks!
  • Developer
    Developer over 9 years
    how you want me to help you?
  • Joe Murray
    Joe Murray over 9 years
    Thanks, Developer! Just go to area51.stackexchange.com/proposals/77367/civicrm and click on Commit button in upper left. If you know of others using CiviCRM encourage them too! Cheers.
  • DOOManiac
    DOOManiac almost 9 years
    Most likely your call to addTTFfont() can be used with less parameters to let the addTTFfont() method automatically detect things: $fontname = TCPDF_FONTS::addTTFfont('/path-to-font/FreeSerifItalic.ttf')‌​;
  • William Turrell
    William Turrell over 8 years
    FWIW it can happen the other way round too, Anecdotally, I converted the gillsans.ttc into individual TTFs (using DfontSplitter) but found while TCPDF_FONTS::addTTFfont worked for semi-bold, bold etc. it returned various errors for the Regular face. The converter at the above URL produced an extra file (gillsans_0.z and gillsans_0.z.cpgz) and that displayed correctly. So try both methods.
  • shukshin.ivan
    shukshin.ivan over 8 years
    It seems that sometimes tcpdf generates wrong .z and .php files of a font. I tried to use online tool xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf out of another answer to generate .z and .php files and my font is working now.
  • Camaleo
    Camaleo over 7 years
    I found out that this one: fonts.snm-portal.com gives much better results :-)
  • bananaCute
    bananaCute about 5 years
    Hi @NicolaAsuni Is the tcpdf_addfont.php also possible to use with OTF font? I tried it but it gives me this error message: '--- Process completed with ERRORS!'
  • kojow7
    kojow7 over 4 years
    There is a link at the top of this page that says: "NOTE: The following information is valid only "foR" the old TCPDF library. The new tc-lib-pdf library uses the new tc-lib-pdf-font library that is able to convert fonts on the fly."
  • Alex Szücs
    Alex Szücs over 3 years
    its a 404 website
  • long
    long about 3 years
    For now, this tool doesn't work well with latin-ext fonts from Google Fonts.
  • Jost
    Jost over 2 years
    Link is dead :/
  • Alfred
    Alfred over 2 years
    @Jost thanks for pointing it out. I have updated the link.