Specific fonts in TCPDF pdf

家住魔仙堡 提交于 2019-12-04 06:55:10

If you want to use a custom font use this tool

http://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.

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

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

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

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

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

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!