简体   繁体   English

在mPDF中加载自己的字体的问题

[英]Problems to load own font in mPDF

I know that question is is asked not the first time but all the other threads havn't helped me to get it work. 我知道这个问题不是第一次被问到,但是其他所有线程都没有帮助我使它正常工作。 I trie to get my own font work in my mPDF call. 我试图在mPDF通话中获得自己的字体。 In read that version 7 I can load my own font without changing the config_fonts.php. 在阅读该版本7的过程中,我可以加载我自己的字体,而无需更改config_fonts.php。 Here is my try: 这是我的尝试:

$mpdf = new \mPDF('utf-8','A4','');

$mpdf->fontDir = './assets/fonts/';
$mpdf->fontdata = array(
    "qanela" => array(
        'R' => "QanelasSoft-Regular.ttf",
        'B' => "QanelasSoft-Bold.ttf",
    ),
);

$mpdf->SetFont('qanela');

with this code I get the errormessage 与此代码,我得到错误消息

mPDF Error - Font is not supported - dejavusanscondensed. mPDF错误-不支持字体-已解压缩。

it is called from 它被称为

mpdf/vendor/mpdf/mpdf/mpdf.php mpdf / vendor / mpdf / mpdf / mpdf.php

on Line 3809 with 在3809行上

if (!isset($this->fontdata[$family][$stylekey]) || !$this->fontdata[$family][$stylekey]) { throw new MpdfException('mPDF Error - Font is not supported - ' . $family . ' ' . $style); 如果(!isset($ this-> fontdata [$ family] [$ stylekey])||!$ this-> fontdata [$ family] [$ stylekey]){抛出新的MpdfException('mPDF错误-不支持字体- '。$ family。''。$ style); } }

Hopefull someone can help me. Hopefull有人可以帮助我。

Cheers 干杯

Your code sample is somehow weird: 您的代码示例有些奇怪:

  • To be able to use fontDir property, you must use mPDF 7.x 若要使用fontDir属性,必须使用mPDF 7.x
  • Your creation of mpdf instance new \\mPDF suggests 6.x - 7.x has a namespaced signature new \\Mpdf\\Mpdf() 您创建的mpdf实例new \\mPDF建议使用6.x-7.x具有命名空间的签名new \\Mpdf\\Mpdf()
  • In 6.x you can set your own font dir via setting _MPDF_SYSTEM_TTFONTS constant: 在6.x中,您可以通过设置_MPDF_SYSTEM_TTFONTS常量来设置自己的字体目录:

define('_MPDF_SYSTEM_TTFONTS', './assets/fonts/');


In read that version 7 I can load my own font without changing the config_fonts.php 在阅读该版本7的过程中,我可以加载我自己的字体而无需更改config_fonts.php

There is no config_fonts.php file in v 7. All changes to configuration can be done in constructor $config parameter or by altering fontdata property of mPDF instance after creation of the object - as you are trying to do. 在v 7中没有config_fonts.php文件。所有对配置的更改都可以在构造函数$config参数中完成,也可以通过在创建对象后更改mPDF实例的fontdata属性来完成-正如您正在尝试的那样。 See below. 见下文。


Also, try to append your font settings to the fontData property instead of overriding its contents: 另外,尝试将字体设置附加到fontData属性,而不是覆盖其内容:

$mpdf->fontdata['qanela'] = 
    array(
        'R' => "QanelasSoft-Regular.ttf",
        'B' => "QanelasSoft-Bold.ttf",
    );

You don't want to override the entire fontdata array (which is what you are doing). 您不想覆盖整个fontdata数组(这就是您正在做的事情)。 Instead, add your new record on the end of it. 而是在其末尾添加新记录。

$mpdf->fontdata['qanelasSof'] = array(
    'R' => "QanelasSoft-Regular.ttf",
    'B' => "QanelasSoft-Bold.ttf",
); 

Then ensure your TTF font files are stored in the ttfonts directory. 然后,确保您的TTF字体文件存储在ttfonts目录中。

mPDF 7.x does not support uppercase fonts. mPDF 7.x不支持大写字体。 In such of situation, you must rename your font name to lowercase 在这种情况下,您必须将字体名称重命名为小写

$mpdf->fontdata = array(
    "qanela" => array(
        'R' => "qanelassoftregular.ttf",
        'B' => "qanelassoftbold.ttf",
    ),
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM