php TCPDF,“无法包含字体定义文件”与 OpenType 字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19385618/
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-25 19:23:47  来源:igfitidea点击:

TCPDF, "Could not include font definition file" with OpenType fonts

phpfontstcpdfopentype

提问by Seroczynski

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.

我是一名 Web 程序员,对字体没有深入了解,并且正在努力让 TCPDF 包含我们的自定义 OpenType 字体。我们购买了不受任何类型 DRM 保护的 OpenType 字体文件 (.oft)。

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:

许多关于此错误消息的问题最终都会得到相同的建议。我已经为 TCPDF (755) 使用的文件夹设置了正确的文件权限,并且使用addTTFfont().ttf TrueType 字体没有问题,如下所示:

$pdf->addTTFfont('/path-to-font/DejaVuSans.ttf', 'TrueTypeUnicode', '', 32);

$pdf->addTTFfont('/path-to-font/DejaVuSans.ttf', 'TrueTypeUnicode', '', 32);

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.

所以我编写了以下代码来包含我们的 OpenFont 类型。该addTTFfont()文档似乎表明支持 OpenTypeUnicode 和 OpenType 参数。

$pdf->addTTFfont('/path-to-font/customfont.otf', 'OpenTypeUnicode', '', 96);

$pdf->addTTFfont('/path-to-font/customfont.otf', 'OpenTypeUnicode', '', 96);

Which results in:

结果是:

TCPDF ERROR: Could not include font definition file:

TCPDF ERROR: Could not include font definition file:

We're using TCPDF v6.0.020 and I've been reading the TCPDF Fonts information pagewith 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.

我们正在使用 TCPDF v6.0.020,到目前为止我一直在阅读TCPDF 字体信息页面,但没有成功。我注意到 TCPDF 也有这个addFont()功能(此处的文档),它似乎更易于使用,因为它不包含对任何字体类型的任何引用。但是,我无法让这个功能与它拥有的小文档一起工作。

Any help would be greatly appreciated.

任何帮助将不胜感激。

回答by JamesG

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.

您确定在调用时遇到该错误addTTFfont()吗?我问的原因是因为我检查了 TCPDF 代码(只是对“无法包含字体定义文件”进行了全局搜索)并且该消息仅出现在addFont()方法中。

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()- 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 use addFont()to add the "raw" versions to the document.

  • AddFont()- This adds a "raw" (ie. already converted) font to the document, which means it is then available for writing text.

  • SetFont()- This sets the font for the next chunk of text that you write.

  • addTTFfont()- 此方法的主要功能是将字体从 TTF(或 OTF)转换为 TCPDF 需要的“原始”版本。理论上,此功能的实现方式可以用作向文档添加字体的主要方法。它将首先检查 tcpdf 字体文件夹,如果转换后的文件不存在,它将继续进行转换。这只是多一点开销,但仍然不是我向文件添加字体的首选方法,因为您需要知道要转换的字体样式才能使该过程成功运行。IMO,最好使用此方法预先转换您计划使用的任何字体,并仅用于addFont()将“原始”版本添加到文档中。

  • AddFont()- 这会向文档添加“原始”(即已转换)字体,这意味着它随后可用于编写文本。

  • SetFont()- 这将设置您编写的下一段文本的字体。

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.

因此,我会使用addTTFfont()将字体预先转换为“原始”版本,然后在实际创建 PDF 的代码中使用addFont()setFont()

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

如果addFont()失败并显示上述错误消息,则表示它找不到字体定义文件。请记住,如果您addFont()使用样式集('i'、'b'、'bi' 等)调用,它所做的只是将其附加到文件名(在扩展名之前)。

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, .zand .ctg.z. So if you converted a font called blah.ttfyou will end up with blah.php, blah.zand 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.zand blahb.ctg.z.

最重要的是,您需要确保您的调用addTTFFont()正在生成“原始”字体文件并将它们保存到您的字体文件夹中。每个样式应该有三个文件,扩展名为.php,.z.ctg.z. 因此,如果您转换了一种名为的字体,blah.ttf您最终会得到blah.php,blah.zblah.ctg.z。如果您转换blah bold.ttf,TCPDF 会发现它是一种粗体,并将“b”附加到文件名的末尾:blahb.php,blahb.zblahb.ctg.z.

Hopefully there will be some nugget in here that will help! Good luck!

希望这里会有一些有用的金块!祝你好运!

回答by Limitless isa

TCPDF New Font add and turkish char problem solved method.

TCPDF 新字体添加和土耳其字符问题解决方法。

Use Converter Link:

使用转换器链接:

  1. http://fonts.snm-portal.com
  2. Download 3 files,
  3. Copy TCPDF-master/fonts
  4. Useage: $pdf->SetFont('roboto', '', 14);
  1. http://fonts.snm-portal.com
  2. 下载3个文件,
  3. 复制 TCPDF-master/字体
  4. 用法:$pdf->SetFont('roboto', '', 14);

enter image description here

在此处输入图片说明

回答by Tom Sawin

To solve this problem with generating font.

通过生成字体来解决这个问题。

Make sure all path are correct.

确保所有路径都正确。

Use this link

使用此链接

Remember to put folder make_fontinside folder of html2pdf

记得把文件夹make_font放在html2pdf

Remember about correct path to: html2pdf.class.php inside folder make_font in index.php

记住正确的路径:index.php 中的文件夹 make_font 内的 html2pdf.class.php

or

或者

If you have other problem - font: Could not include font definition file:

如果您有其他问题 - 字体:无法包含字体定义文件:

It's a problem of correct name of your new font.

这是新字体名称正确的问题。

For example using when generating pdf:

例如在生成 pdf 时使用:

Correct: $html2pdf->addFont('lato i', '', 'latoi.php');

正确的: $html2pdf->addFont('lato i', '', 'latoi.php');

Incorrect: $html2pdf->addFont('lato', '', 'latoi.php');

不正确: $html2pdf->addFont('lato', '', 'latoi.php');

Remember there are only 2 files generated:

请记住,只生成了 2 个文件:

latoi.php

latoi.z

latoi.php

拉托伊兹

You don't need any ctg file.

您不需要任何 ctg 文件。

I hope it will help

我希望它会有所帮助

回答by Dion

In my case the problem was, that the fonts path wasn't writable. I fixed it by changing permissions of the folder: var_dump(K_PATH_FONTS);

就我而言,问题是字体路径不可写。我通过更改文件夹的权限来修复它:var_dump(K_PATH_FONTS);