简体   繁体   English

FontAwesome 图标不会显示在使用 mPDF 生成的 PDF 上

[英]FontAwesome Icons won't Show on Generated PDF using mPDF

I am using mPDF in generating payslips.我正在使用 mPDF 生成工资单。 However, the icons in the payslip aren't showing once it is generated.但是,工资单中的图标一旦生成就不会显示。 It only leaves a blank space just like this:它只留下一个空白,就像这样:

在此处输入图片说明

Icons should show on those highlighted spots.图标应该显示在那些突出显示的地方。 So far, here's what I've done:到目前为止,这是我所做的:

I am using Yii2 PHP framework and here's my action controller:我正在使用 Yii2 PHP 框架,这是我的动作控制器:

public function actionPdf($id)
{
    $model = $this->findModel($id);
    $earnings = EarningDetails::find()->where(['payslip_id' => $model->_id, 'status' => 1])->all();
    $deductions = DeductionDetails::find()->where(['payslip_id' => $model->_id, 'status' => 1])->all();
    $html = $this->render('view', [
        'model' => $model,
        'earnings' => $earnings,
        'deductions' => $deductions,
    ]);
    $mpdf = new mPDF('c','A5-L','0','',0,4,1,1,0,0);
    $mpdf->allow_charset_conversion = true; 
    $mpdf->charset_in = 'windows-1252';
    $mpdf->SetTopMargin(0);
    $user_password = User::find()->where(['_id' => $model->user_id ])->one(); 
    $password = $user_password->fname.$user_password->lname;
    $mpdf->SetProtection(array(), $password, $password);
    $mpdf->WriteHTML($html);
    $mpdf->Output('Payslip.pdf', 'D');
    exit;        
}

Am I missing something?我错过了什么吗? Please let me know.请告诉我。

Encoding issues aside, this could be a couple of things. 除了编码问题,这可能是两件事。 Firstly, you need to integrate FontAwesome with your MPDF installation. 首先,您需要将FontAwesome与MPDF安装集成在一起。 Secondly, you need to consider how you're speficiying the glyph in HTML. 其次,您需要考虑如何在HTML中使用字形。

Installing FontAwesome in mPDF 在mPDF中安装FontAwesome

Download or clone FontAwesome from https://github.com/FortAwesome/Font-Awesome and copy fonts/fontawesome-webfont.ttf into your MPDF ttfonts/ directory. https://github.com/FortAwesome/Font-Awesome下载或克隆FontAwesome,然后将fonts / fontawesome-webfont.ttf复制到MPDF ttfonts /目录中。

In your MDPF config_fonts.php file, add the following lines to $this->fontdata : 在您的MDPF config_fonts.php文件中,将以下行添加到$this->fontdata

 /* FontAwesome */
    "fontawesome" => array(
        'R' => "fontawesome-webfont.ttf"
        ),

Adding the glyph in HTML 在HTML中添加字形

You need to keep in mind that the CSS :before pseudo-selector commonly used to add FontAwesome glyphs to HTML doesn't work in mPDF. 您需要记住,通常在mPDF中无法使用CSS :before伪选择器,该伪选择器通常用于将FontAwesome字形添加到HTML。

Bad: 坏:

<i class="fa fa-smile-o"></i>

... because this FontAwesome CSS rule doesn't work in mPDF: ...因为此FontAwesome CSS规则在mPDF中不起作用:

.fa-smile-o:before {
  content: "\f118";
}

Good: 好:

<i class="fa">&#xf118;</i>

You can get the unicode code point for each glyph by clicking on it on the FontAwesome Icon List, but the Cheatsheet is more convenient for this. 您可以通过点击它的FontAwesome图标列表获取每个字形的Unicode代码点,但的cheatsheet是这个更方便。

You can try to use the re-mapped fontawesome 4.7 (free) font to #0021-#02E5 characters, and use it as it was regular FONT (ascii).您可以尝试使用重新映射的 fontawesome 4.7(免费)字体到 #0021-#02E5 字符,并像常规字体 (ascii) 一样使用它。

You can download the ready-to-use font here: http://mdb-blog.blogspot.com/2021/12/using-fontwesome-in-php-fpdf.html您可以在此处下载即用型字体: http : //mdb-blog.blogspot.com/2021/12/using-fontwesome-in-php-fpdf.html

Note that the example works for FPDF, but it is easly can be made for any tool :)

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

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