TCPDF,“无法包含字体定义文件"使用 OpenType 字体
我是一名 Web 程序员,对字体没有深入了解,正在努力让 TCPDF 包含我们的自定义 OpenType 字体.我们购买了不受任何 DRM 保护的 OpenType 字体文件 (.oft).
I am a web programmer with no in-depth knowledge of fonts and am struggling to get TCPDF to include our custom OpenType font. We have bought OpenType font files (.oft), which are not protected by any kind of DRM.
很多关于此错误消息的问题最终都得到了相同的建议.我已经为 TCPDF (755) 使用的文件夹设置了正确的文件权限,并且使用 addTTFfont()
来包含 .ttf TrueType 字体没有问题,如下所示:
A lot of questions regarding this error message end up getting the same advice. I've set the correct file permissions for the folders used by TCPDF (755) and I have no trouble using the addTTFfont()
to including .ttf TrueType fonts like so:
$pdf->addTTFfont('/path-to-font/DejaVuSans.ttf', 'TrueTypeUnicode', '', 32);
所以我编写了以下代码来包含我们的 OpenFont 类型.addTTFfont()
文档似乎表明支持 OpenTypeUnicode 和 OpenType 参数.
So I've made up the following code to include our OpenFont type. The addTTFfont()
documentation seems to indicate support of OpenTypeUnicode and OpenType parameters.
$pdf->addTTFfont('/path-to-font/customfont.otf', 'OpenTypeUnicode', '', 96);
结果:
TCPD 错误:无法包含字体定义文件:
我们使用的是 TCPDF v6.0.020,我一直在阅读 TCPPDF 字体信息页面 到目前为止运气不佳.我注意到 TCPDF 也有 addFont()
函数 (此处的文档) 使用起来似乎更明显,因为它不包含对任何字体类型的任何引用.但是,我无法使用它所拥有的小文档来使用此功能.
We're using TCPDF v6.0.020 and I've been reading the TCPDF Fonts information page with no luck so far. I've noticed TCPDF also has the addFont()
function (documentation here) which seems more obvious to use as it does not include any reference to any font type. However, I was unable to get this function to work with the little documentation it has.
任何帮助将不胜感激.
推荐答案
您确定在调用 addTTFfont()
时遇到该错误吗?我问的原因是因为我检查了 TCPDF 代码(只是对无法包含字体定义文件"进行了全局搜索)并且该消息仅出现在 addFont()
方法中.
Are you sure that you are getting that error when calling addTTFfont()
? The reason I ask is because I checked the TCPDF code (just did a global search on "Could not include font definition file") and that message only appears in the addFont()
method.
这两种方法可能有点混乱,但我几个月前给自己写了一些笔记,下面我会部分复制,希望对你有所帮助:
These two methods can be a bit confusing, but I wrote myself some notes a few months ago, which I will partially reproduce below in the hope that it helps you somehow:
addTTFfont()
- 此方法的主要功能是将字体从 TTF(或 OTF)转换为 TCPDF 需要的原始"版本.理论上,此功能的实现方式可以用作向文档添加字体的主要方法.它将首先检查 tcpdf 字体文件夹,如果转换后的文件不存在,它将继续进行转换.这只是多一点开销,但仍然不是我向文件添加字体的首选方法,因为您需要知道要转换的字体样式才能使该过程成功运行.IMO,最好使用此方法预先转换您计划使用的任何字体,只需使用addFont()
将原始"版本添加到文档中.
addTTFfont()
- The primary function of this method is to convert a font from TTF (or OTF) to the "raw" version that TCPDF needs. The way this function is implemented you could, in theory, use it as your primary method of adding fonts to a document. It will check the tcpdf font folder first and if the converted files aren't there it will go ahead and do the conversion. It is only a little bit more overhead, but still not my preferred method of adding fonts to files, as you need to know what style of font you are converting for the process to even work successfully. IMO, it is better to use this method to pre-convert any fonts that you plan on using and simply useaddFont()
to add the "raw" versions to the document.
AddFont()
- 这会向文档添加原始"(即已转换)字体,这意味着它随后可用于编写文本.
AddFont()
- This adds a "raw" (ie. already converted) font to the document, which means it is then available for writing text.
SetFont()
- 这会为您编写的下一段文本设置字体.
SetFont()
- This sets the font for the next chunk of text that you write.
所以我会使用 addTTFfont()
将字体预转换为原始"版本,然后使用 addFont()
和 setFont()
在实际创建 PDF 的代码中.
So I would use addTTFfont()
to pre-convert the font to the "raw" version, then use addFont()
and setFont()
in the code that actually creates the PDF.
如果 addFont()
失败并显示上述错误消息,则意味着它找不到字体定义文件.请记住,如果您使用样式集('i'、'b'、'bi' 等)调用 addFont()
,它所做的只是将其附加到文件名(在扩展名之前)).
If addFont()
is failing with the error message above, it means it cannot find the font definition file. Keep in mind that if you call addFont()
with style set ('i', 'b', 'bi', etc), all it does is append this to the file name (before the extension).
最重要的是,您需要确保对 addTTFFont()
的调用正在生成原始"字体文件并将它们保存到您的字体文件夹中.每个样式应该有三个文件,扩展名为 .php
、.z
和 .ctg.z
.所以如果你转换了一个名为 blah.ttf
的字体,你最终会得到 blah.php
、blah.z
和 blah.ctg.z
.如果你转换 blah bold.ttf
,TCPDF 会判断它是粗体,并在文件名的末尾附加b":blahb.php
,blahb.z
和 blahb.ctg.z
.
Most importantly, you need to make sure that your call to addTTFFont()
is producing the "raw" font files and saving them into your fonts folder. There should be three files per style, with extensions of .php
, .z
and .ctg.z
. So if you converted a font called blah.ttf
you will end up with blah.php
, blah.z
and blah.ctg.z
. If you convert blah bold.ttf
, TCPDF will figure out that it is a bold font and append the 'b' to the end of the file names: blahb.php
, blahb.z
and blahb.ctg.z
.
希望这里会有一些有用的金块!祝你好运!
Hopefully there will be some nugget in here that will help! Good luck!
相关文章