简体   繁体   English

如何使用mPDF生成PDF并向其添加自定义Google字体?

[英]How to generate PDF using mPDF and add custom Google font to it?

I am generating PDF using mPDF PHP library. 我正在使用mPDF PHP库生成PDF。 I am able to successfully generate it using the default font configuration. 我能够使用默认字体配置成功生成它。 However, I want to render a Google font in my PDF. 但是,我想在我的PDF中呈现Google字体。 I tried using the steps mentioned in this link , but it did not work. 我尝试使用此链接中提到的步骤,但它不起作用。 Below is the code I use. 以下是我使用的代码。

$mPDFO = new mPDF('utf-8', 'A4', 0, '', 10, 10, 10, 0, 0, 0, 'L');

Can anyone help me using Google Font in mPDF ? 任何人都可以帮我在mPDF中使用Google字体吗?

As mentioned in mPDF's documentation, it's not possible to use remote fonts directly by referencing them in the HTML. 正如mPDF的文档中所提到的,通过在HTML中引用远程字体是不可能的。 Follow the below steps to use custom fonts: 按照以下步骤使用自定义字体:

  1. Download the fonts & upload them to mPDF's fonts directory /ttfonts 下载字体并将其上传到mPDF的字体目录/ttfonts

  2. Declare the font-family you need to use in config_fonts.php under: $this->fontdata 声明你需要在config_fonts.php使用的font-family: $this->fontdata

  3. Now comes the main part. 现在是主要部分。 You need to mention the font-family in the instance that you create to call mPDF's object which is what you missed, like so: 您需要在您创建的实例中提及font-family来调用mPDF的对象,这是您错过的,如下所示:

    $mPDFO = new mPDF('utf-8', 'A4', 0, 'Source Sans Pro', 10, 10, 10, 0, 0, 0, 'L');

  4. Finally call the SetFont method like $mPDFO->SetFont('Source Sans Pro'); 最后调用SetFont方法,如$mPDFO->SetFont('Source Sans Pro'); just below your instance 就在您的实例下方

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

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