将字体添加到mPDF [英] adding font to mPDF

查看:434
本文介绍了将字体添加到mPDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  TTF文件C在使用mPDF类生成PDF时出现以下错误: :/wamp/www/inc/mpdf/ttfonts/verdana.ttf:校验和无效20f65173c11表:DSIG(预计65173c11)

我已将字体文件上传到我的 ttfonts 目录,并在 config_fonts.php 像这样:

 verdana=> array(
'R'=>verdana.ttf,
'B'=>verdanab.ttf,
'I'=>verdanai.ttf,
'BI'=>verdanaz.ttf,
),

当我在配置设置中打开字体错误报告时,我只看到错误。当我关闭错误报告时,将生成PDF,但正在使用的字体不是Verdana。



任何想法我做错了什么?

解决方案

基于@ hrvoje-golcic的答案,这里有一个改进的,不太脏的方式来添加字体到mPDF,而不需要编辑 config_fonts.php 。我使用Laravel,我用composer安装了mPDF。


  1. 按照作者的建议,定义一个名为在初始化 mPDF 之前使用_MPDF_TTFONTPATH 作为你的 ttfonts 文件夹的路径至少存在5.3)。
  2. vendor / mpdf / mpdf / ttfonts 文件夹复制到您控制的位置
  3. 将您的自定义字体与其他文件一起添加到该文件夹​​中。

  4. 将您的配置添加到 mPDF 实例上的fontdata 属性。



    ttfonts 文件夹大约有90MB,所以仍然可能有更好的方法,但是您必须复制所有的字体,因为原来的配置添加它们。请参阅此答案底部的作曲家脚本备选案文。



    重要提示: rel =noreferrer / blockquote>

    下面是一个例子:

    pre $ if $($ defined('_ MPDF_TTFONTPATH' )){
    //一个绝对路径是首选的,所需的尾部斜线:
    define('_ MPDF_TTFONTPATH',realpath('fonts /'));
    //使用Laravel的resource_path函数的示例:
    // define('_ MPDF_TTFONTPATH',resource_path('fonts /'));


    函数add_custom_fonts_to_mpdf($ mpdf,$ fonts_list){

    $ fontdata = [
    'sourcesanspro'=> [
    'R'=> 'SourceSansPro-Regular.ttf',
    'B'=> 'SourceSansPro-Bold.ttf',
    ],
    ];

    foreach($ fontdata as $ f => $ fs){
    //添加到数据列表
    $ mpdf-> fontdata [$ f] = $ fs;

    //添加到可用字体数组
    foreach(['R','B','I','BI'] as $ style){
    if(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);



    作曲者安装后脚本



    复制所有的字体并将它们添加到git,一个方便的解决方法,使用作曲家后安装脚本可以为你做。



    首先,确保文件夹在那里你想复制字体存在,并在其中创建一个 .gitignore ,其中包含以下内容:

     * 
    !.gitignore
    !SourceSansPro-Regular.ttf
    !SourceSansPro-Bold.ttf

    除了 .gitignore 文件和你想添加的字体外,
    $ b

    接下来,将以下脚本添加到 composer.json 文件中:

    < pre code $b $ b $$ b $b $ c $ -f vendor / mpdf / mpdf / ttfonts / * resources / fonts /

    post-update-cmd:[
    cp -f vendor / mpdf / mpdf / ttfonts / * resources / fonts /
    ]
    }



    笔记



    这个测试可以和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.

    1. As suggested by the author, define a constant named _MPDF_TTFONTPATH before initializing mPDF with the value as the path to your ttfonts folder (this constant exists since at least 5.3).
    2. Copy the vendor/mpdf/mpdf/ttfonts folder to a location that you control (outside of the vendor folder).
    3. Add your custom fonts to that folder along with the others.
    4. Add your configuration to the fontdata property on the mPDF 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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