Specific fonts in TCPDF pdf
Asked Answered
M

7

6

Can somebody tell me what i am doing wrong? I need Arial font in my pdf generated by TCPDF. First I've tried to use that : 1) I got Arial from windows fonts caltalog and put it in TCPDF directory. 2) Next I wrote in script :

$fontname = $pdf->addTTFfont('../lib/tcpdf/arial.ttf', '', '', 32);

After that in tcpdf/fonts appears 3 files (arial.ctg.z , arial.php and arial.z). I thought that everything was ok but if in TCPDF i use this font by:

$pdf->SetFont('arial', '', 16);

Font in document is indeed arial but without polish specific sings (ąęłżńź)

I've tried also prepare font by yourself : I downloaded ttf2afm and makefontuni.php script then in command line I wrote this:

ttf2ufm -a arial.ttf
php -q makefontuni.php arial.ttf arial.ufm

that command gave me also 3 files (arial.ctg.z , arial.php and arial.z) but final situation is the same like before.

I've admit that all data writing to pdf is in UTF-8 and TCPDF construct is like this :

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'utf-8', false);

I don't know what am i doing wrong. :(

Mejias answered 14/1, 2012 at 9:12 Comment(0)
E
16

If you want to use a custom font use this tool

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

when you get the generated files just move them to the /fonts directory and with the same name they have, set the font-name attribute.

Exteroceptor answered 17/10, 2012 at 18:7 Comment(3)
I just want to add that 2 files need to be added in library .php and .zSomatotype
Wow. It works better than TCPDF_FONTS::addTTFfont. I tried to use addTTFfont, but the font didn't work. When I substituted .z and .php files of my font, it works.Lemmy
I found out that this one: fonts.snm-portal.com gives much better results :-)Crosswind
D
4

No need to add any font, use the Dejavu Sans Font which has all the UTF-8 characters built-in and the TCPDF has it already..

$pdf->SetFont('dejavusans', '', 14, '', true);

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

Digitalize answered 14/1, 2012 at 9:31 Comment(2)
Yes you are right but I need Arial font not dejavusans and here is the problemMejias
@Miro Markaravanes : It works for special characters but does not show bold effect.Horbal
G
2
$fontname = TCPDF_FONTS::addTTFfont(FCPATH.'/assets/css/fonts/arial-unicode-ms.ttf');

This is what I use to include custom font to TCPDF. You need only .ttf file of the font. Add it in folder on your choice on the server and run this code once. ( I run it first time on export ) Then you can comment this row and the font would be there.

In order to add it to the exporter you should add it as font with:

$pdf->addFont('your-font-name', '', 10, '', false);

And if you want it to be default:

$pdf->setFont('your-font-name', '', 10, '', false);

If you don't know what is the actual name of the font to use in PDF:

echo $fontname; 

This would give you the specific name of the font to use in the exported file.

After you run that command once the TCPDF creates all the file needed in its font folder and it is ready to use from now on.

If you want to re-add the same font (modified) you need to delete your font files in tcpdf/fonts/your-font-name. and run this command again to re-add them.

Giaour answered 18/1, 2017 at 8:45 Comment(0)
E
0

Hm, are you sure your Arial has all UTF-8 characters? I followed instructions here http://www.tcpdf.org/fonts.php and it worked. What I had problems was that I was only able to add Regular Font - as soon as I added Bold or Italic and then change from one another, all characters turned to dots.

So at the moment I'm only using my Regular font and for Bold I use 'dejavusans' (thanks to Miro). My code:

$fontname = $pdf->addTTFfont('/lib/tcpdf/fonts/myfont-Regular.ttf','TrueTypeUnicode','');  
$pdf->SetFont($fontname, '', 8, '', true); 
Ellata answered 2/2, 2012 at 10:15 Comment(0)
E
0

I had the same error i was able to fixed it putting the next, after de line:

 $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, 
        true, 'UTF-8', false);
 $fontname = $pdf->addTTFfont('tcpdf/fonts/arial.ttf', '', '', 32);
 $pdf->SetFont('arial', '', 16);`
Epizoon answered 6/2, 2013 at 16:10 Comment(0)
M
0

I think it will help you to fix the character issue.

$pdf->SetFont('freeserif', '', 12);

Above font-family which supports the UTF-8 characters.

Manche answered 7/10, 2013 at 10:19 Comment(0)
C
0

Is better if you convert the font to ctg.z/z/php files. You can convert ttf fonts with http://fonts.palettize.me and then put uncompressed result in fonts folder into tcpdf class. Then you can add it with $pdf->SetFont('jameelnoorinastaleeq', 'BI', 20);

Chronicles answered 6/4, 2021 at 15:4 Comment(2)
this seems to not-work. "no fonts created"Lardon
Try again, be sure is a ttf font and sometimes the filename in uppercase may have some troublesChronicles

© 2022 - 2024 — McMap. All rights reserved.