How to implement custom fonts in TCPDF
Asked Answered
N

15

50

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??

Nannettenanni answered 10/3, 2011 at 17:43 Comment(0)
R
92

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()!

Resht answered 2/10, 2011 at 12:37 Comment(6)
Related Answer: #19386118Bawl
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');Inebriety
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.Colier
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!'Nitrogenize
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."Empiricism
getting error Class "App\Http\Controllers\TCPDF_FONTS" not foundFishworm
P
30

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

Pulque answered 17/10, 2012 at 18:3 Comment(4)
how come this works sometimes but returns empty font definition files the other times?Ashliashlie
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.Woundwort
I found out that this one: fonts.snm-portal.com gives much better results :-)Torrens
For now, this tool doesn't work well with latin-ext fonts from Google Fonts.Dearing
W
10

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!

Windstorm answered 14/1, 2015 at 18:57 Comment(0)
B
5

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 :)

Botswana answered 23/10, 2013 at 15:44 Comment(3)
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!Prelacy
how you want me to help you?Botswana
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.Prelacy
R
3

I don't know anything about tcpdf or php, but I found this:

http://www.tcpdf.org/examples/example_033.phps

Apparently you just use the font's name, not the file name.


Strike one!

Okay, how about this page. To prep a font to be used by TCPDF, you have to run the file through a command line utility and a PHP script.

$ ttf2ufm -a -F myfont.ttf

And then:
$ php -q makefont.php myfont.ttf myfont.ufm
or
MakeFont(string $fontfile, string $fmfile [, boolean $embedded [, $enc="cp1252" [, $patch=array()]]])

ttf2ufm is distributed with TCPDF in the TCPDF/fonts directory.

Research answered 11/3, 2011 at 0:22 Comment(0)
H
3

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);
Homeroom answered 19/6, 2015 at 10:1 Comment(0)
M
2

I found the addTTFfont method a little finicky (ok, probably didn't try very hard) and the online tool mentioned above works for FPDF but misses the ctg.z file that TCPDF requires.

This one - http://fonts.snm-portal.com/ - generates the 3 required files (.php, .z and .ctg.z) for TCPDF. Convert the TTF, upload the three files to your fonts folder and you're good to go.

Mariande answered 28/5, 2015 at 23:58 Comment(0)
B
2

Create new font by the following script after adding php in your path

php /path_to_tcpdf_directory/tools/tcpdf_addfont.php -i font_name.ttf,font_nameb.ttf,font_namei.ttf

Then use the font by following code

$pdf->SetFont('Font_name');

Name of the font can be found in the /path_to_tcpdf_directory/fonts/font_name.php

To add php into your environment variables, refer How to set the env variable for PHP?

Berkelium answered 23/5, 2022 at 11:45 Comment(0)
N
1

Latest TCPDF supports custom fonts.

Documentation about using custom fonts with TCPDF can be read here.

Nannettenanni answered 9/4, 2012 at 16:55 Comment(2)
Link is dead :/Rowe
@Rowe thanks for pointing it out. I have updated the link.Nannettenanni
Y
1

the best way i have been tried and worked 100% put your TTF font in fonts folder and then use this constant K_PATH_FONTS + FONT NAME

   $font1 = $this->pdf->addTTFfont(K_PATH_FONTS . 'arial.ttf', 'TrueTypeUnicode', '', 8);
   $this->pdf->SetFont($font1, '', 15, '', false);
Yahwistic answered 13/1, 2013 at 12:32 Comment(0)
V
0

There is no point in using addTTFfont() if you don't have the .ttf file.

And the whole point is: if there is NO ttf file, how can someone use addTTFfont() function?

For example, there is no cid0cs file in font/ directory (TCPDF 6.0.20), only cid0cs.php, which is NOT a font file.

Vanessa answered 18/6, 2013 at 8:59 Comment(0)
S
0

I wasn't able to find addTTFFont() in the latest release of tcpdf. However, I was able to include a custom TTF file by myself. Within the tcpdf folder is a folder named tools which include a PHP file called tcpdf_addfont.php.

I symlinked it to my home directory (you can omit this step) and run it like this: shell ./addfont.php -b -t TrueTypeUnicode -f 32 -i myfont.ttf

That's it, it will dump something similar to this: ```

Converting fonts for TCPDF: *** Output dir set to /Users/pascalraszyk/XXX/XXX/pdf/vendor/tecnick.com/tcpdf/fonts/ +++ OK : /Users/pascalraszyk/XXX/XXX/pdf/myfont.ttf added as myfont Process successfully completed! ```

Note: There are more examples within the tools folder for various font types. I used composer to checkout tcpdf.

As long as you put your font definition files in the tcpdf/fonts folder you should be good to go!

I used HTML2PDF which uses tcpdf under the hood and it worked like a charm.

Sponson answered 25/6, 2015 at 13:2 Comment(0)
M
0

I had this problem despite having all the right files. I had to change the file names to all lower case

Moke answered 10/11, 2016 at 19:6 Comment(0)
F
0

TCPDF: How to add new custom font in tcpdf

  1. Download required font's ttf file
  2. Open http://www.fpdf.org/makefont website in the browser and upload the ttf font on this website
  3. Download two files customfontname.php and customfontname.z
  4. Copy these two files into /yourproject/vendor/tecnickcom/tcpdf/fonts/ folder
  5. include / set the font in your script before tcpdf html code $pdf->SetFont('customfontname', '', 12);

Alternate font generation website: https://fonts.palettize.me

Fishworm answered 22/12, 2023 at 14:11 Comment(0)
Z
-2

When was not possible convert font GothamRounded standard way, I was looking for another tool and only this one http://fonts.snm-portal.com/ worked for me.

Zumwalt answered 8/1, 2017 at 10:31 Comment(1)
its a 404 websiteMitman

© 2022 - 2024 — McMap. All rights reserved.