Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specific fonts in TCPDF pdf

Tags:

php

pdf

utf-8

tcpdf

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. :(

like image 978
Karol85 Avatar asked Jan 14 '12 09:01

Karol85


3 Answers

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.

like image 165
Yannis Avatar answered Oct 29 '22 20:10

Yannis


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

like image 38
Miro Markaravanes Avatar answered Oct 29 '22 19:10

Miro Markaravanes


$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.

like image 23
Mitko Delibaltov Avatar answered Oct 29 '22 21:10

Mitko Delibaltov