php 如何在 TCPDF 中设置不同的字体

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

How to set different Fonts in TCPDF

phptcpdf

提问by TJR

I am looking for a solution to set more than just one font for my PDF document which is created with tcpdf.

我正在寻找一种解决方案,为使用 tcpdf 创建的 PDF 文档设置不止一种字体。

I want to do something like this:

我想做这样的事情:

$pdf->SetFont('verdana_bold', 'B', 12);
$pdf->SetFont('verdana', '', 12);

I need for my document a bold font and a regular font. The example above doesn't work. When I switch the two lines the text is all bold. When I use the example above the text is just regular.

我的文档需要加粗字体和常规字体。上面的例子不起作用。当我切换两行时,文本全部为粗体。当我使用上面的示例时,文本只是常规的。

I want to set the font-weightwith regular css stylesheets.

我想font-weight用常规的 css 样式表设置。

Hope you have a solution.

希望你有解决办法。

回答by Andi

You can use your custom fonts inside html like this:

您可以在 html 中使用自定义字体,如下所示:

$fontname=$pdf->addTTFfont('path/myfont.ttf', '', '', 32);
//echo $fontname;

$html='<span style="font-family:'.$fontname'.;font-weight:bold">my text in bold</span>: my normal text';
$pdf->writeHTMLCell($w=0,$h=0,$x=11,$y=201,$html,$border=0,$ln=0,$fill=false,$reseth=true,$align='L',$autopadding=false);

回答by powtac

Convert Verdana for TCPDF usage:

为 TCPDF 使用转换 Verdana:

$fontname = $pdf->addTTFfont('/path-to-font/verdana.ttf', 'TrueTypeUnicode', '', 32);
  • Make sure the fonts folder is writeable
  • Have you set the path K_PATH_FONTS constant to the fonts in your config/tcpdf_config.php ?
  • Read trough TCPDF Fonts.
  • 确保字体文件夹是可写的
  • 您是否将路径 K_PATH_FONTS 常量设置为 config/tcpdf_config.php 中的字体?
  • 通读TCPDF 字体

回答by Developer

The lines below will generate 3 files in your fonts folder

以下几行将在您的字体文件夹中生成 3 个文件

rotisserifi56.php, rotisserifi56.ctg, rotisserifi56.rar

rotisserifi56.php, rotisserifi56.ctg, rotisserifi56.rar

use this to generate the file

使用它来生成文件

$fontname = $this->pdf->addTTFfont('fonts/Rotis Serif Italic 56.ttf', 'TrueTypeUnicode', '', 32);


// use the font
$this->pdf->SetFont($fontname, '', 14, '', false);

Now, use the fonts like this:

现在,使用这样的字体:

$this->pdf->AddFont('rotisserifi56', '', 'rotisserifi56.php');
$this->pdf->SetFont('rotisserifi56');

回答by th3penguinwhisperer

I know this question is pretty old but I had the same problem and fixed it.

我知道这个问题已经很老了,但我遇到了同样的问题并修复了它。

What you CAN do but SHOULDN'T do is:

你可以做但不应该做的是:

$pdf->SetFont('verdana_bold', 'B', 12);
$pdf->SetFont('verdana', '', 12);

What you basically do here is define 2 fonts. One with the name verdana and one with name verdana_bold. While you specify the B for bold it can't find this ttf. Because TCPDF basically checks for a file in the fonts dir called verdana_boldb.ttf. This doesn't exist so it takes the verdana_bold.ttf (which at first sight seems to be the correct behavior).

您在这里所做的基本上是定义 2 种字体。一种名为 verdana,一种名为 verdana_bold。当您指定 B 为粗体时,它无法找到此 ttf。因为 TCPDF 基本上会检查字体目录中名为 verdana_boldb.ttf 的文件。这不存在,所以它需要 verdana_bold.ttf (乍一看似乎是正确的行为)。

For me the issue got noticable after I tried to use both bold and non-bold styles in a table and I either only got the whole table in bold or the whole table in non-bold (removing or adding the B style specifier doesn't make a difference).

对我来说,在我尝试在表格中同时使用粗体和非粗体样式后,问题变得很明显,我要么只将整个表格以粗体显示,要么以非粗体显示整个表格(删除或添加 B 样式说明符不会做出改变)。

What you SHOULD do:

你应该做什么:

Add the new font type:

添加新的字体类型:

$fontname = TCPDF_FONTS::addTTFfont($fontfile, 'TrueType', '', 32);

When you want to use the font:

当你想使用字体时:

$pdf->SetFont('verdana', '', 10, '', false);

When you want items in bold in a HTML cell use the html b tag:

当您希望 HTML 单元格中的项目以粗体显示时,请使用 html b 标签:

<b>myvalue</b>

You can check in the fonts directory if you have the verdanab.ttf file.

如果您有 verdanab.ttf 文件,您可以检查字体目录。

$ ls fonts/verdanab.
verdanab.ctg.z  verdanab.php    verdanab.z

I hope this helps someone else :)

我希望这对其他人有帮助:)

回答by Vaibhavi Azrekar

steps to inlcude custom font:

包含自定义字体的步骤:

  1. you can find the .ttf file of the custom font you want from c://windows/fonts directory.
  2. copy that file into the fonts folder located at tcpdf/fonts
  3. use $pdf->addTTFfont('C://wamp/www/projectname/...path to .ttf file', 'TrueTypeUnicode', '', 32);
  4. $pdf->SetFont('custom_font_name');
  1. 您可以从 c://windows/fonts 目录中找到您想要的自定义字体的 .ttf 文件。
  2. 将该文件复制到位于 tcpdf/fonts 的 fonts 文件夹中
  3. $pdf->addTTFfont('C://wamp/www/projectname/...path to .ttf file', 'TrueTypeUnicode', '', 32);
  4. $pdf->SetFont('custom_font_name');

your custom font is ready to use.

您的自定义字体已准备好使用。

回答by TJR

I just fixed my problem. The problem was, that the fonts must be named in the right way. verdana_bold is wrong - it must be verdanab. Then i just have to register the verdana font and tcpdf grab automaticly the verdanab.ttf for the bold version of this font.

我刚刚解决了我的问题。问题是,字体必须以正确的方式命名。verdana_bold 是错误的——它必须是 verdanab。然后我只需要注册 verdana 字体和 tcpdf 自动抓取该字体的粗体版本的 verdanab.ttf。