adding font to mPDF(将字体添加到 mPDF)
问题描述
当我尝试使用 mPDF 类生成 PDF 时出现以下错误:
TTF 文件C:/wamp/www/inc/mpdf/ttfonts/verdana.ttf":无效校验和 20f65173c11 表:DSIG(预期为 65173c11)我已将字体文件上传到我的 ttfonts
目录并在 config_fonts.php
中定义字体,如下所示:
"verdana" =>大批('R' =>"verdana.ttf",'B' =>"verdanab.ttf",'我' =>"verdanai.ttf",'BI' =>"verdanaz.ttf",),
我只在配置设置中打开字体错误报告时看到错误.当我关闭错误报告时,会生成 PDF,但使用的字体不是 Verdana.
知道我做错了什么吗?
解决方案 基于@hrvoje-golcic 的回答,这里有一种改进的、不那么脏的方法,无需编辑 config_fonts.php
即可将字体添加到 mPDF.我正在使用 Laravel,我使用 Composer 安装了 mPDF.
- 按照作者的建议,在初始化
mPDF
之前定义一个名为 _MPDF_TTFONTPATH
的常量,并将该值作为 ttfonts
文件夹的路径(这至少从 5.3 开始就存在常量). - 将
vendor/mpdf/mpdf/ttfonts
文件夹复制到您控制的位置(在 vendor 文件夹之外). - 将您的自定义字体与其他字体一起添加到该文件夹中.
- 将您的配置添加到
mPDF
实例的 fontdata
属性中.
<块引用>
注意:ttfonts
文件夹大约有 90MB,所以可能还有更好的方法,但是您必须复制所有字体,因为原始配置添加了它们.请参阅此答案底部的作曲家脚本替代方案.
重要提示: CSS font-family 将被转换 到小写 + nospaces 所以Source Sans Pro"将成为sourceanspro.
这是一个例子:
if (!defined('_MPDF_TTFONTPATH')) {//首选绝对路径,需要尾部斜杠:定义('_MPDF_TTFONTPATH', realpath('fonts/'));//使用 Laravel 的 resource_path 函数的例子://定义('_MPDF_TTFONTPATH', resource_path('fonts/'));}函数 add_custom_fonts_to_mpdf($mpdf, $fonts_list) {$字体数据 = ['sourcesanspro' =>['R' =>'SourceSansPro-Regular.ttf','B' =>'SourceSansPro-Bold.ttf',],];foreach ($fontdata as $f => $fs) {//添加到字体数据数组$mpdf->fontdata[$f] = $fs;//添加到可用字体数组foreach (['R', 'B', 'I', 'BI'] 作为 $style) {如果 (isset($fs[$style]) && $fs[$style]) {//警告:常规样式没有后缀!浪费时间:2$mpdf->available_unifonts[] = $f .修剪($style,'R');}}}$mpdf->default_available_fonts = $mpdf->available_unifonts;}$mpdf = new mPDF('UTF-8', 'A4');add_custom_fonts_to_mpdf($mpdf);$mpdf->WriteHTML($html);
Composer 安装后脚本
不是复制所有字体并将它们添加到 git,而是使用 Composer 安装后脚本的一个方便的解决方法可以为您做到这一点.
首先确保你要复制字体的文件夹存在,并在其中创建一个.gitignore
,内容如下:
<预><代码>*!.gitignore!SourceSansPro-Regular.ttf!SourceSansPro-Bold.ttf
这将忽略除 .gitignore
文件和您要添加的字体之外的所有内容.
接下来,将以下脚本添加到您的 composer.json
文件中:
脚本":{安装后命令":["cp -f vendor/mpdf/mpdf/ttfonts/* 资源/字体/"],post-update-cmd":["cp -f vendor/mpdf/mpdf/ttfonts/* 资源/字体/"]}
注意事项
这经过测试可以与 6.1 一起使用.
在 7.x 中,作者实现了一种优雅的添加外部字体的方式.
I'm getting the following error when I try and generate a PDF using the mPDF class:
TTF file "C:/wamp/www/inc/mpdf/ttfonts/verdana.ttf": invalid checksum 20f65173c11 table: DSIG (expected 65173c11)
I've uploaded the font files to my ttfonts
directory and defined the font in config_fonts.php
like this:
"verdana" => array(
'R' => "verdana.ttf",
'B' => "verdanab.ttf",
'I' => "verdanai.ttf",
'BI' => "verdanaz.ttf",
),
I only see the error when I turn on font error reporting in the config settings. When I turn error reporting off, the PDF is generated, but the font being used is not Verdana.
Any idea on what I'm doing wrong?
Based on @hrvoje-golcic answer, here's an improved and less dirty way to add fonts to mPDF without editing config_fonts.php
. I'm using Laravel, I installed mPDF using composer.
- As suggested by the author, define a constant named
_MPDF_TTFONTPATH
before initializingmPDF
with the value as the path to yourttfonts
folder (this constant exists since at least 5.3). - Copy the
vendor/mpdf/mpdf/ttfonts
folder to a location that you control (outside of the vendor folder). - Add your custom fonts to that folder along with the others.
- Add your configuration to the
fontdata
property on themPDF
instance.
Heads up: The
ttfonts
folder has around 90MB so there's still might be a better way, but you have to copy all the fonts since the original config adds them. See composer script alternative at the bottom of this answer.IMPORTANT: CSS font-family will be transformed to lowercase + nospaces so "Source Sans Pro" will become sourcesanspro.
Here's an example:
if (!defined('_MPDF_TTFONTPATH')) {
// an absolute path is preferred, trailing slash required:
define('_MPDF_TTFONTPATH', realpath('fonts/'));
// example using Laravel's resource_path function:
// define('_MPDF_TTFONTPATH', resource_path('fonts/'));
}
function add_custom_fonts_to_mpdf($mpdf, $fonts_list) {
$fontdata = [
'sourcesanspro' => [
'R' => 'SourceSansPro-Regular.ttf',
'B' => 'SourceSansPro-Bold.ttf',
],
];
foreach ($fontdata as $f => $fs) {
// add to fontdata array
$mpdf->fontdata[$f] = $fs;
// add to available fonts array
foreach (['R', 'B', 'I', 'BI'] as $style) {
if (isset($fs[$style]) && $fs[$style]) {
// warning: no suffix for regular style! hours wasted: 2
$mpdf->available_unifonts[] = $f . trim($style, 'R');
}
}
}
$mpdf->default_available_fonts = $mpdf->available_unifonts;
}
$mpdf = new mPDF('UTF-8', 'A4');
add_custom_fonts_to_mpdf($mpdf);
$mpdf->WriteHTML($html);
Composer post-install script
Instead of copying all the fonts and adding them to git, a handy workaround using a composer post-install script can do that for you.
First of all, make sure the folder where you wish to copy the fonts exists, and create a .gitignore
in it, with the following contents:
*
!.gitignore
!SourceSansPro-Regular.ttf
!SourceSansPro-Bold.ttf
This will ignore everything except the .gitignore
file and the fonts you wish to add.
Next, add the following scripts to your composer.json
file:
"scripts": {
"post-install-cmd": [
"cp -f vendor/mpdf/mpdf/ttfonts/* resources/fonts/"
],
"post-update-cmd": [
"cp -f vendor/mpdf/mpdf/ttfonts/* resources/fonts/"
]
}
Notes
This was tested to work with 6.1.
In 7.x, the author implemented an elegant way to add external fonts.
这篇关于将字体添加到 mPDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将字体添加到 mPDF
基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- HTTP 与 FTP 上传 2021-01-01