php TCPDF pdf 中的特定字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8861260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:39:29  来源:igfitidea点击:

Specific fonts in TCPDF pdf

phppdfutf-8tcpdf

提问by Karol85

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 :

有人可以告诉我我做错了什么吗?我需要在由 TCPPDF 生成的 pdf 中使用 Arial 字体。首先,我尝试使用它: 1) 我从 windows fonts caltalog 中获取了 Arial,并将其放入 TCPDF 目录中。2)接下来我在脚本中写道:

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

之后在 tcpdf/fonts 中出现 3 个文件(arial.ctg.z、arial.php 和 arial.z)。我认为一切正常,但如果在 TCPDF 中我通过以下方式使用此字体:

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

Font in document is indeed arial but without polish specific sings (????ń?)

文档中的字体确实是 arial,但没有波兰语特定的歌声 (??????)

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

我也试过自己准备字体:我下载了 ttf2afm 和 makefontuni.php 脚本,然后在命令行中我写了这个:

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.

该命令还给了我 3 个文件(arial.ctg.z、arial.php 和 arial.z),但最终情况和以前一样。

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

我承认所有写入 pdf 的数据都是 UTF-8 格式,而 TCPDF 结构是这样的:

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

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

我不知道我做错了什么。:(

回答by Yannis

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

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.

当您获得生成的文件时,只需将它们移动到 /fonts 目录并使用相同的名称,设置 font-name 属性。

回答by Miro Markaravanes

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

无需添加任何字体,使用内置了所有 UTF-8 字符的 Dejavu Sans 字体,并且 TCPDF 已经有了它。

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

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

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

回答by Mitko Delibaltov

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

这是我用来将自定义字体包含到 TCPDF 中的方法。您只需要字体的 .ttf 文件。将它添加到您在服务器上选择的文件夹中并运行此代码一次。(我第一次在导出时运行它)然后你可以评论这一行,字体就会在那里。

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:

如果您不知道要在 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.

在您运行该命令后,一旦 TCPDF 在其字体文件夹中创建了所有需要的文件,它就可以从现在开始使用了。

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.

如果要重新添加相同的字体(已修改),则需要删除 tcpdf/fonts/your-font-name 中的字体文件。并再次运行此命令以重新添加它们。

回答by jazkat

Hm, are you sure your Arial has all UTF-8 characters? I followed instructions here http://www.tcpdf.org/fonts.phpand 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.

嗯,你确定你的 Arial 全是 UTF-8 字符吗?我按照这里的说明http://www.tcpdf.org/fonts.php并且它起作用了。我遇到的问题是我只能添加常规字体 - 一旦我添加了粗体或斜体,然后相互更改,所有字符都变成了点。

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

所以目前我只使用我的常规字体,而对于粗体,我使用“dejavusans”(感谢 Miro)。我的代码:

$fontname = $pdf->addTTFfont('/lib/tcpdf/fonts/myfont-Regular.ttf','TrueTypeUnicode','');  
$pdf->SetFont($fontname, '', 8, '', true); 

回答by ileana

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

我有同样的错误,我能够修复它,在 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);

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

回答by Mohammed Nagoor

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

我认为这将帮助您解决角色问题。

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

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

以上字体系列支持 UTF-8 字符。