如何在 html2pdf PHP 程序中添加 TTF 字体

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

How to add TTF font to html2pdf PHP program

phptrue-type-fontshtml2pdf

提问by netdjw

I'm trying to use HTML2PDF 4.03 with this code:

我正在尝试将 HTML2PDF 4.03 与此代码一起使用:

<?php
$content = "..."; # my HTML code
require_once(dirname(__FILE__).'/html2pdf_v4.03/html2pdf.class.php');
$html2pdf = new HTML2PDF('P','A4','en', true, 'utf-8', array(15,20,15,20) );
# here I'm trying to add my arial.ttf
$html2pdf->pdf->AddTTFFont('arial.ttf');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');
?>

Now the program die with this:

现在程序死了:

PHP Fatal error:  Call to undefined method HTML2PDF_myPdf::AddTTFFont()

How can I add TTF font to my PDF file?

如何将 TTF 字体添加到我的 PDF 文件中?

回答by o11y_75

I have managed to add 1 custom font to my setup using the following method.

我已设法使用以下方法将 1 种自定义字体添加到我的设置中。

First convert the .ttf file to 3 separate files (.php .z and .ufm) using the following font converterPlace the 3 files that are generated by this system into the fonts folder in TCPDF.

首先使用以下字体转换器将.ttf 文件转换为3 个单独的文件(.php .z 和.ufm) 将本系统生成的3 个文件放入TCPDF 的fonts 文件夹中。

Now you can set the default font for your PDF using the following command

现在您可以使用以下命令为您的 PDF 设置默认字体

$html2pdf->setDefaultFont("the_name_you_called_your_font");

This was fairly simple to get working, I am having issues using 2 seperate fonts though via this method. I'll figure it out though

这很容易开始工作,但通过这种方法我在使用 2 种单独的字体时遇到了问题。我会想办法的

回答by Ignacio

To expand on the selected answer (by o11y_75) when you convert your fonts, you need to use a specific name to include also the bold and italic variants. That way, you only add one font definition like this

要在转换字体时扩展所选答案(由 o11y_75 提供),您需要使用特定名称来包括粗体和斜体变体。这样,您只需添加一个这样的字体定义

$html2pdf->AddFont('opensans', 'normal', 'opensans.php');
$html2pdf->setDefaultFont('opensans');

When you convert the fonts, name them, for example, like these:

转换字体时,为它们命名,例如,如下所示:

default: opensans
bold: opensansb
italic: opensansi
bold italic: opensansbi

notice that behind the original name, you add b, i and bi on each case. I have found no documentation on this issue, but I followed the nomenclature found on the fonts that already came with TCPDF and it worked.

请注意,在原始名称后面,您在每种情况下都添加了 b、i 和 bi。我没有找到关于这个问题的文档,但我遵循了在 TCPDF 已经附带的字体上找到的命名法并且它有效。

回答by Guillaume Pommier

If you want to add multiple fonts, just use :

如果要添加多种字体,只需使用:

$html2pdf->addFont('opensansregular', '', 'opensansregular');
$html2pdf->addFont('opensansbold', '', 'opensansbold');

I would suggest that you don't use special chars with the font converter specified above.

我建议您不要在上面指定的字体转换器中使用特殊字符。

Then in your CSS simply type :

然后在你的 CSS 中简单地输入:

<style type="text/css">
<!--
.uppercase {
    text-transform: uppercase;
}
* {
    font-family: opensansregular;
}
h1, h2, h3, strong {
    font-family: opensansbold;
}
-->
</style>