简体   繁体   English

如何在 MPDF 中使用 Font Awesome?

[英]How to use Font Awesome with MPDF?

I am using Zend Framework and creating PDF with mpdf.我正在使用 Zend Framework 并使用 mpdf 创建 PDF。
I am trying to use fontawesome for denoting some of the articles but the fonts of font awesome are not rendering properly below is the code .我正在尝试使用 fontawesome 来表示一些文章,但是 fontawesome 的字体没有正确呈现,下面是代码。

$stylesheet =  file_get_contents("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css");
$stylesheet .= file_get_contents("https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css");

$this->mpdf->WriteHTML($stylesheet,1);
$this->mpdf->WriteHTML($html,2);
$this->mpdf->allow_charset_conversion = true; 
$this->mpdf->charset_in = 'windows-1252';
$this->mpdf->Output();

The code I am using in the html我在 html 中使用的代码

<span class="company-name">&#xf21b; name of the company</span>

Thanks in advance.提前致谢。

EDIT: Nowadays, this is an obsolete method, and should only be used on legacy systems.编辑:如今,这是一种过时的方法,只能用于遗留系统。

I recommend the answer below, from @Arie , to modify the mPDF font in its recent versions and/or if it was installed by Composer, passing the parameters when instantiating the class.我推荐以下来自@Arie 的答案,以修改其最新版本中的 mPDF 字体和/或如果它是由 Composer 安装的,则在实例化类时传递参数。


The easy way:简单的方法:

  1. Copy fontawesome-webfont.ttf file into /mPDF/ttfonts/ directory.fontawesome-webfont.ttf文件复制到/mPDF/ttfonts/目录中。

  2. Edit /mPDF/config_fonts.php , search the array started by $this->fontdata and add to it:编辑/mPDF/config_fonts.php ,搜索$this->fontdata开始的数组并添加:

"fontawesome" => array(
    'R' => "fontawesome-webfont.ttf",
),
  1. Change your document CSS properly, with your new font family.使用新的字体系列正确更改文档 CSS。 Eg.:例如。:
$stylesheet = '.company-name { font-family: fontawesome; }';
  1. When you instantiate the class, let the first parameter in blank:实例化类时,将第一个参数留空:

Eg.:例如。:

$mpdf = new mPDF();

or或者

$mpdf = new mPDF('', 'A4');

I am tested with mPDF 6.0 and it worked.我用 mPDF 6.0 进行了测试并且它有效。

Also, the mPDF manual explain how to do it with more options: Fonts in mPDF 6.x此外,mPDF 手册解释了如何使用更多选项进行操作: mPDF 6.x 中的字体

A more recent solution for Font Awesome 5.x and mPDF 7.x, when you don't want to edit the source files/dirs from mPDF: https://mpdf.github.io/fonts-languages/fonts-in-mpdf-7-x.html Font Awesome 5.x 和 mPDF 7.x 的更新解决方案,当您不想从 mPDF 编辑源文件/目录时: https ://mpdf.github.io/fonts-languages/fonts-in- mpdf-7-x.html

My use case, this offers usage of mPDF supplied fonts, fontawesome and another custom font (ibmplex in this case).我的用例,这提供了使用 mPDF 提供的字体、fontawesome 和另一种自定义字体(在这种情况下为 ibmplex)。 Note that when supplying a font name such as 'ibmplex' and 'fontawesome', they are lowercase.请注意,在提供诸如“ibmplex”和“fontawesome”之类的字体名称时,它们是小写的。 To avoid confusion why a font doesn't work, I'd advice using lower case and no spaces for the names.为了避免混淆为什么字体不起作用,我建议使用小写字母并且名称中没有空格。

$defaultConfig = (new Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];

$defaultFontConfig = (new Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];

$mpdf = new \Mpdf\Mpdf([
    'fontDir' => array_merge($fontDirs, [
        __DIR__ . '/../../resources/fonts',
    ]),
    'fontdata' => $fontData + [
        'ibmplex' => [
            'R' => 'IBMPlexSans-Regular.ttf',
            'B' => 'IBMPlexSans-Bold.ttf',
            'I' => 'IBMPlexSans-Italic.ttf',
        ],
        'fontawesome' => [
            'R' => 'fa-solid-900.ttf'
        ],
    ],
    'default_font' => 'ibmplex',
    'format' => 'A4'
]);

Then you can use然后你可以使用

<span style="font-family: fontawesome;">&#xf3ed;</span>

As per https://fontawesome.com/cheatsheet -- note that f3ed is the actual icon in this case.根据https://fontawesome.com/cheatsheet - 请注意,在这种情况下, f3ed是实际图标。

Actually, if you have installed mPdf with composer, add the following lines into file located in vendor\\mpdf\\mpdf\\src\\Config\\FontVariables.php实际上,如果您使用 composer 安装了 mPdf,请将以下几行添加到位于 vendor\\mpdf\\mpdf\\src\\Config\\FontVariables.php 的文件中

"fontawesome" => [ 
                    'R' => "fontawesome-webfont.ttf",
                ],

inside the 'fontdata' section在“fontdata”部分内

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

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