【问题标题】:FontAwesome Icons won't Show on Generated PDF using mPDFFontAwesome 图标不会显示在使用 mPDF 生成的 PDF 上
【发布时间】:2015-08-07 19:26:05
【问题描述】:

我正在使用 mPDF 生成工资单。但是,工资单中的图标一旦生成就不会显示。它只留下一个空白,就像这样:

图标应显示在这些突出显示的位置上。 到目前为止,这是我所做的:

我正在使用 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;        
}

我错过了什么吗?请告诉我。

【问题讨论】:

  • 正如我在对C# ItextSharp Fontawesome Icons ( currency ) - PDF 的回答中所解释的,您正在对可能需要 UNICODE 的符号使用 Windows-1252 编码。
  • 那你是什么意思?这个问题与c#有关
  • 它是法语的,但请看一下此博客中的屏幕截图:Utilisation de polices: PDF et iText。您会看到某些屏幕截图中缺少某些字符,因为使用了错误的编码。双字节 Unicode 字符 \uf0d6 与两个单独的单字节字符之间存在差异,其值为 240 (0xF0) 和 214 (0xD6)。也许这就是你的问题。
  • 对不起,我不太明白。你建议我做什么?
  • 你可以开始学习更多关于字体和编码的知识。向我们展示 PDF。也许您甚至没有使用 Fontawesome 字体(它可能不会显示在“文档”>“属性”下的“字体”面板中)。

标签: php pdf icons mpdf


【解决方案1】:

抛开编码问题不谈,这可能是几件事。首先,您需要将 FontAwesome 与您的 MPDF 安装集成。其次,您需要考虑如何在 HTML 中指定字形。

在 mPDF 中安装 FontAwesome

https://github.com/FortAwesome/Font-Awesome 下载或克隆 FontAwesome 并将 fonts/fontawesome-webfont.ttf 复制到您的 MPDF ttfonts/ 目录中。

在您的 MDPF config_fonts.php 文件中,将以下行添加到 $this->fontdata

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

在 HTML 中添加字形

您需要记住,通常用于将 FontAwesome 字形添加到 HTML 的 CSS :before 伪选择器在 mPDF 中不起作用。

不好:

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

...因为这个 FontAwesome CSS 规则在 mPDF 中不起作用:

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

好:

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

您可以通过在 FontAwesome 图标列表中单击每个字形获取 unicode 代码点,但Cheatsheet 更方便。

【讨论】:

  • 这个答案拯救了我的夜晚。谢谢你。我希望 mpdf 能做得更好。
【解决方案2】:

您可以尝试使用重新映射的 fontawesome 4.7(免费)字体到#0021-#02E5 字符, 并像常规 FONT (ascii) 一样使用它。

您可以在此处下载即用型字体: 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 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2018-02-04
    • 2016-04-25
    • 1970-01-01
    相关资源
    最近更新 更多