首页 文章

如何在yii2中将自定义字体添加到kartik yii-mpdf

提问于
浏览
1

我知道如何使它在本地环境中工作,我将字体.ttf添加到 /vendor/mpdf/mpdf/ttfonts ,然后添加

$this->fontdata = array(
"calibri" => array(
'R' => 'calibri.ttf',
'B' => 'calibrib.ttf',
)
...

/vendor/mpdf/config-fonts.php

所以现在我可以在我的pdf中使用Calibri字体,但是我应该正确使用它不仅在本地和 composer update 之后?

1 回答

  • 0

    我找到的解决方案:

    1)在 /your/path/to/fonts/dir 中放入.ttf字体,添加 custom_config.php 以及此内容:

    $this->fontdata["calibri"] = [
        'R' => 'calibri.ttf',
        'B' => 'calibrib.ttf',
    ];
    

    2)在common / configs中设置自定义字体的路径,并使用自定义配置文件

    'mpdfCustomFontsPath' => /your/path/to/custom_config.php,
    'mpdfCustomFonts' => /your/path/to/fonts/dir,
    

    3)在创建kartik / Pdf实例之前,您的代码中有些内容执行此操作:

    $customFontsConfig = Yii::$app->params['mpdfCustomFontsPath'];
    $customFonts = Yii::$app->params['mpdfCustomFonts'];
    define("_MPDF_SYSTEM_TTFONTS_CONFIG", $customFontsConfig);
    define("_MPDF_SYSTEM_TTFONTS", $customFonts);
    

    这将工作,因为mpdf将在处理html到pdf时检查 _MPDF_SYSTEM_TTFONTS_CONFIG_MPDF_SYSTEM_TTFONTS

相关问题