【问题标题】:Dompdf does not render custom fontsDompdf 不呈现自定义字体
【发布时间】:2018-07-27 12:13:29
【问题描述】:

我发送给 dompdf 的 $html 字符串在 echo`ed 时给了我一个很好的字体。 我已经尝试过:

  • 手动将字体添加到 dompdf/dompdf/lib/fonts
  • 编译并保存 .afm 和 .ufm 文件到 dompdf/dompdf/lib/fonts
  • 使用文件下载和作曲家卸载并重新安装 dompdf
  • 下载 dompdf\utils 并在 cmd 中运行 load_fonts 脚本

这是我调用 dompdf 的代码

$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$dompdf = new Dompdf('A4', $options);
$dompdf->loadHtml($html, 'UTF-8');
$dompdf->setPaper('A4');
$dompdf->render();
$output = $dompdf->output();

这是它在 $html 中的外观,我称之为@font-face

<?php $link = yii\helpers\Url::base(true) . '/web/css/fonts/Roboto-Bold.ttf';
?>

@font-face {
   font-family: 'Roboto';
   font-style: normal;
   font-weight: normal;
   src: url( <?php echo $link ?>) format('truetype');
}

【问题讨论】:

    标签: php yii2 dompdf


    【解决方案1】:

    试试下面

    首先在views创建view.php文件并添加font-facelike

    <?php $link = \Yii::getAlias('@webroot') . '/css/fonts/Roboto-Bold.ttf'; ?>
    <html>
        <head>
            <style type="text/css">
                @font-face {
                    font-family:roboto;
                    src:url(" <?php echo $link ?> ") format('truetype');
                    font-weight:normal;
                    font-style:normal;
                }
            </style>
        </head>
        <body>
            <p style="font-family:roboto">Sample Text Here</p>
        </body>
    </html>
    

    注意:检查加载字体文件路径是否正确。

    controller 中使用renderPartial() 加载视图文件并调用Dompdf 喜欢

    public function actionPrintDemo() {
        $html = $this->renderPartial('view');
        $options = new Options();
        $options->set('isHtml5ParserEnabled', true);
        $dompdf = new Dompdf('A4', $options);
        $dompdf->loadHtml($html, 'UTF-8');
        $dompdf->setPaper('A4');
        $dompdf->render();
        $output = $dompdf->output();
    }
    

    【讨论】:

    • 感谢您的意见。不幸的是,getAllias 的调用给了我“不允许加载本地资源:”
    猜你喜欢
    • 1970-01-01
    • 2012-09-16
    • 2021-10-07
    • 2017-08-28
    • 2020-06-12
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 2022-08-08
    相关资源
    最近更新 更多