可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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. :(
回答1:
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.
回答2:
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
回答3:
$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.
回答4:
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);
回答5:
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);
回答6:
I think it will help you to fix the character issue.
$pdf->SetFont('freeserif', '', 12);
Above font-family which supports the UTF-8 characters.