如何在 TCPDF 中实现自定义字体

发布于 2024-10-21 18:02:52 字数 111 浏览 8 评论 0原文

在 TCPDF 中,只有几种字体可供选择来创建 pdf 文件。我想将 Tahoma 设置为我的 pdf 字体。如何将 Tahoma 包含在 TCPDF 中?

In TCPDF, there are only a few fonts to choose from, to create pdf files. I want to set Tahoma as my pdf font. How can I include Tahoma in TCPDF??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

鲸落 2024-10-28 18:02:52

最新的 TCPDF 版本使用 addTTFfont() 方法自动将字体转换为 TCPDF 格式。例如:

// convert TTF font to TCPDF format and store it on the fonts folder
$fontname = TCPDF_FONTS::addTTFfont('/path-to-font/FreeSerifItalic.ttf', 'TrueTypeUnicode', '', 96);

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

有关更多信息和示例,请查看TCPDF 字体文档页面

注意:转换字体后,TCPDF 不再需要 TTF 文件或上述对 addTTFfont() 的调用!

The latest TCPDF version automatically convert fonts into TCPDF format using the addTTFfont() method. For example:

// convert TTF font to TCPDF format and store it on the fonts folder
$fontname = TCPDF_FONTS::addTTFfont('/path-to-font/FreeSerifItalic.ttf', 'TrueTypeUnicode', '', 96);

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

For further information and examples, please check the TCPDF Fonts documentation page.

NOTE: Once the font has been converted, TCPDF no longer requires the TTF file or the above call to addTTFfont()!

默嘫て 2024-10-28 18:02:52

我在网上发现了一个非常好的工具。
您唯一需要做的就是上传 .ttf 文件,然后下载文件并复制到 /fonts 文件夹中。

https://www.xml-convert .com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf

I have discovered a very good tool online.
The only thing you need to do is to upload your .ttf file and then download the files and copy then into the /fonts folder.

https://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf

病毒体 2024-10-28 18:02:52

addTTFfont 方法在 TCPDF 主类上不可用,因此以下内容对我有用。

// convert TTF font to TCPDF format and store it on the fonts folder
$fontname = TCPDF_FONTS::addTTFfont('pathto/arial.ttf', 'TrueTypeUnicode', '', 96);

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

希望这有帮助!

The addTTFfont method is not available on TCPDF main class so following worked for me.

// convert TTF font to TCPDF format and store it on the fonts folder
$fontname = TCPDF_FONTS::addTTFfont('pathto/arial.ttf', 'TrueTypeUnicode', '', 96);

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

Hope this helps!

寒冷纷飞旳雪 2024-10-28 18:02:52

下面的行将在你的字体文件夹中生成 3 个文件
1.rotisserifi56.php
2.rotisserifi56.ctg
3.rotisserifi56.rar

    use this to generate the required php and other files
$fontname = $this->pdf->addTTFfont('D:/wamp/www/projectname/sites/all/modules/civicrm/packages/tcpdf/fonts/Rotis Serif Italic 56.ttf', 'TrueTypeUnicode', '', 32);

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

现在,

使用这样的字体:

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

--希望这对某些人有帮助:)

the below lines will generate 3 files in ur fonts folder
1.rotisserifi56.php
2.rotisserifi56.ctg
3.rotisserifi56.rar

    use this to generate the required php and other files
$fontname = $this->pdf->addTTFfont('D:/wamp/www/projectname/sites/all/modules/civicrm/packages/tcpdf/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');

--hope this helps some one :)

江南月 2024-10-28 18:02:52

我对 tcpdf 或 php 一无所知,但我发现了这个:

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

显然你只是使用字体的名称,而不是文件名。


打击一击!

好的,这个页面怎么样。要准备 TCPDF 使用的字体,您必须通过命令行实用程序和 PHP 脚本运行该文件。

$ ttf2ufm -a -F myfont.ttf

然后:
$ php -q makefont.php myfont.ttf myfont.ufm

MakeFont(字符串 $fontfile, 字符串 $fmfile [, 布尔值 $embedded [, $enc="cp1252" [, $patch=array()]]])

ttf2ufm 与 TCPDF 一起发布在 TCPDF/fonts 目录中。

I don't know anything about tcpdf or php, but I found this:

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

Apparently you just use the font's name, not the file name.


Strike one!

Okay, how about this page. To prep a font to be used by TCPDF, you have to run the file through a command line utility and a PHP script.

$ ttf2ufm -a -F myfont.ttf

And then:
$ php -q makefont.php myfont.ttf myfont.ufm
or
MakeFont(string $fontfile, string $fmfile [, boolean $embedded [, $enc="cp1252" [, $patch=array()]]])

ttf2ufm is distributed with TCPDF in the TCPDF/fonts directory.

吃→可爱长大的 2024-10-28 18:02:52

首先从 http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf 并将所有三个移动到包含 .ttf 文件的同一目录中。
然后使用这个:

$pdf->AddFont(path-to/universe.ttf','',path-to/universe.php');
$pdf->SetFont(path-to/universe.ttf','',10);

First create .php ,.afm,.z from http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf and move all three in same directory containing .ttf file.
then Use This :

$pdf->AddFont(path-to/universe.ttf','',path-to/universe.php');
$pdf->SetFont(path-to/universe.ttf','',10);
比忠 2024-10-28 18:02:52

我发现 addTTFfont 方法有点挑剔(好吧,可能没有努力尝试),上面提到的在线工具适用于 FPDF,但缺少 TCPDF 所需的 ctg.z 文件。

这个 - http://fonts.snm-portal.com/ - 生成 3 个必需的文件( .php、.z 和 .ctg.z)用于 TCPDF。转换 TTF,将三个文件上传到您的字体文件夹,然后就可以开始了。

I found the addTTFfont method a little finicky (ok, probably didn't try very hard) and the online tool mentioned above works for FPDF but misses the ctg.z file that TCPDF requires.

This one - http://fonts.snm-portal.com/ - generates the 3 required files (.php, .z and .ctg.z) for TCPDF. Convert the TTF, upload the three files to your fonts folder and you're good to go.

抱猫软卧 2024-10-28 18:02:52

在路径中添加 php 后,通过以下脚本创建新字体

php /path_to_tcpdf_directory/tools/tcpdf_addfont.php -i font_name.ttf,font_nameb.ttf,font_namei.ttf

然后通过以下代码使用字体 字体

$pdf->SetFont('Font_name');

名称可以在 /path_to_tcpdf_directory/fonts/font_name.php 中找到

将 php 添加到环境变量中,请参阅 如何设置 PHP 的环境变量?

Create new font by the following script after adding php in your path

php /path_to_tcpdf_directory/tools/tcpdf_addfont.php -i font_name.ttf,font_nameb.ttf,font_namei.ttf

Then use the font by following code

$pdf->SetFont('Font_name');

Name of the font can be found in the /path_to_tcpdf_directory/fonts/font_name.php

To add php into your environment variables, refer How to set the env variable for PHP?

一生独一 2024-10-28 18:02:52

我尝试过并且100%有效的最好方法
将您的 TTF 字体放入字体文件夹中,然后使用此常量 K_PATH_FONTS + FONT NAME

   $font1 = $this->pdf->addTTFfont(K_PATH_FONTS . 'arial.ttf', 'TrueTypeUnicode', '', 8);
   $this->pdf->SetFont($font1, '', 15, '', false);

the best way i have been tried and worked 100%
put your TTF font in fonts folder and then use this constant K_PATH_FONTS + FONT NAME

   $font1 = $this->pdf->addTTFfont(K_PATH_FONTS . 'arial.ttf', 'TrueTypeUnicode', '', 8);
   $this->pdf->SetFont($font1, '', 15, '', false);
挽容 2024-10-28 18:02:52

最新的 TCPDF 支持自定义字体。

有关在 TCPDF 中使用自定义字体的文档可以在此处阅读。

Latest TCPDF supports custom fonts.

Documentation about using custom fonts with TCPDF can be read here.

去了角落 2024-10-28 18:02:52

如果没有 .ttf 文件,则使用 addTTFfont() 是没有意义的。

重点是:如果没有 ttf 文件,人们如何使用 addTTFfont() 函数?

例如,font/目录中没有cid0cs文件(TCPDF 6.0.20),只有cid0cs.php,它不是字体文件

There is no point in using addTTFfont() if you don't have the .ttf file.

And the whole point is: if there is NO ttf file, how can someone use addTTFfont() function?

For example, there is no cid0cs file in font/ directory (TCPDF 6.0.20), only cid0cs.php, which is NOT a font file.

寂寞美少年 2024-10-28 18:02:52

我在最新版本的 tcpdf 中找不到 addTTFFont()。
不过,我自己可以包含自定义 TTF 文件。 tcpdf 文件夹中有一个名为 tools 的文件夹,其中包含一个名为 tcpdf_addfont.php 的 PHP 文件。

我将它符号链接到我的主目录(您可以省略此步骤)并像这样运行它:
外壳
./addfont.php -b -t TrueTypeUnicode -f 32 -i myfont.ttf

就是这样,它会转储类似这样的内容:
````

<块引用>
<块引用>

转换 TCPDF 字体:
*** 输出目录设置为 /Users/pascalraszyk/XXX/XXX/pdf/vendor/tecnick.com/tcpdf/fonts/
+++ 好的:/Users/pascalraszyk/XXX/XXX/pdf/myfont.ttf 添加为 myfont
过程成功完成!
````


注意: tools 文件夹中有更多针对各种字体类型的示例。
我使用 Composer 来检查 tcpdf。

只要将字体定义文件放入 tcpdf/fonts 文件夹中,就可以开始了!

我使用了 HTML2PDF,它在底层使用了 tcpdf,它的工作方式就像一个魅力。

I wasn't able to find addTTFFont() in the latest release of tcpdf.
However, I was able to include a custom TTF file by myself. Within the tcpdf folder is a folder named tools which include a PHP file called tcpdf_addfont.php.

I symlinked it to my home directory (you can omit this step) and run it like this:
shell
./addfont.php -b -t TrueTypeUnicode -f 32 -i myfont.ttf

That's it, it will dump something similar to this:
```

Converting fonts for TCPDF:
*** Output dir set to /Users/pascalraszyk/XXX/XXX/pdf/vendor/tecnick.com/tcpdf/fonts/
+++ OK : /Users/pascalraszyk/XXX/XXX/pdf/myfont.ttf added as myfont
Process successfully completed!
```

Note: There are more examples within the tools folder for various font types.
I used composer to checkout tcpdf.

As long as you put your font definition files in the tcpdf/fonts folder you should be good to go!

I used HTML2PDF which uses tcpdf under the hood and it worked like a charm.

少女的英雄梦 2024-10-28 18:02:52

尽管拥有所有正确的文件,我还是遇到了这个问题。我必须将文件名全部更改为小写

I had this problem despite having all the right files. I had to change the file names to all lower case

勿忘心安 2024-10-28 18:02:52

TCPDF:如何在 tcpdf 中添加新的自定义字体

  1. 下载所需字体的 ttf 文件
  2. 打开 http://在浏览器中 /www.fpdf.org/makefont 网站并在该网站上上传 ttf 字体
  3. 下载两个文件 customfontname.phpcustomfontname.z
  4. 复制这些将两个文件放入 /yourproject/vendor/tecnickcom/tcpdf/fonts/ 文件夹中,
  5. 在 tcpdf html 代码之前在脚本中包含/设置字体 $pdf->SetFont('customfontname', '' , 12);

备用字体生成网站:
https://fonts.palettize.me

TCPDF: How to add new custom font in tcpdf

  1. Download required font's ttf file
  2. Open http://www.fpdf.org/makefont website in the browser and upload the ttf font on this website
  3. Download two files customfontname.php and customfontname.z
  4. Copy these two files into /yourproject/vendor/tecnickcom/tcpdf/fonts/ folder
  5. include / set the font in your script before tcpdf html code $pdf->SetFont('customfontname', '', 12);

Alternate font generation website:
https://fonts.palettize.me

挥剑断情 2024-10-28 18:02:52

当不可能以标准方式转换字体 GothamRounded 时,我一直在寻找另一种工具,只有这个工具 http://fonts .snm-portal.com/ 为我工作。

When was not possible convert font GothamRounded standard way, I was looking for another tool and only this one http://fonts.snm-portal.com/ worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文