如何在TCPDF中添加自定义字体? [英] How do you add custom fonts in TCPDF?

查看:1572
本文介绍了如何在TCPDF中添加自定义字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为使用TCPDF生成的pdf添加自定义字体。我可能会错过一些东西,但文档似乎已经过时。他们引用了 addTTFfont()函数,但是我认为它已经被弃用了,并且不再存在于最新版本的TCPDF中。



我读了我需要转换的ttf文件,并把它放在字体文件夹,所以我跑:

  php / tcpdf / tools / tcpdf_addfont.php -i php / font / rumpelstiltskin-webfont.ttf 

这些文件现在在字体文件夹中:

  rumpelstiltskinwebfont.ctg.z 
rumpelstiltskinwebfont.z
rumpelstiltskinwebfont.php

然后我尝试添加字体:

  $ pdf-> addFont('rumpelstiltskin'); 
$ pdf-> SetFont('rumpelstiltskin','',14,'',false);

但是我收到错误:

  TCPDF错误:无法包含字体定义文件:rumpelstiltskin 


解决方案

我发现我的问题,我几乎在那里。

这是一步一步的:



首先使用TCPDF工具文件夹中的tcpdf_addfont.php工具字体转换您的字体:

  php / tcpdf / tools / tcpdf_addfont.php -i php / font / rumpelstiltskin-webfont.ttf 

将生成所需的文件并将其放在TCPDF字体文件夹中。检查字体文件夹并复制字体的名称,在我的情况下它是 rumpelstiltskinwebfont



在您的代码使用字体文件的名字设置字体,并写下一行文字:

  $ pdf-> SetFont('rumpelstiltskinwebfont', ',14,',false); 
$ pdf-> Write(0,'Fill text','',0,'',true,0,false,false,0);

就是这样。希望这有助于某人。 :)

I would like to add a custom font to a pdf I'm generating using TCPDF. I might be missing something but the docs seem to be out dated. They are referencing the addTTFfont() function but I think it's been deprecated and no longer exists in the latest version of TCPDF.

I read that I need to convert the ttf file and put it in the fonts folder so I ran:

php/tcpdf/tools/tcpdf_addfont.php -i php/font/rumpelstiltskin-webfont.ttf

and it generated these files which are now in the fonts folder:

rumpelstiltskinwebfont.ctg.z
rumpelstiltskinwebfont.z
rumpelstiltskinwebfont.php

Then I tried to add the font:

$pdf->addFont('rumpelstiltskin');
$pdf->SetFont('rumpelstiltskin', '', 14, '', false);

but I'm getting an error:

TCPDF ERROR: Could not include font definition file: rumpelstiltskin

解决方案

I figured out my issue, I was almost there.

Here is a step by step:

First convert your font using the tcpdf_addfont.php tool font in the TCPDF tools folder:

php/tcpdf/tools/tcpdf_addfont.php -i php/font/rumpelstiltskin-webfont.ttf

This will generate the required files and put them in the TCPDF fonts folder. Check the fonts folder and copy the name of the font, in my case it was rumpelstiltskinwebfont.

In your code set the font using the font file's name and write a line of text:

$pdf->SetFont('rumpelstiltskinwebfont', '', 14, '', false);
$pdf->Write(0, 'Fill text', '', 0, '', true, 0, false, false, 0);

That's it. Hope this helps someone. :)

这篇关于如何在TCPDF中添加自定义字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆