【问题标题】:@font-face or custom font does not work in PHP pdf generator class@font-face 或自定义字体在 PHP pdf 生成器类中不起作用
【发布时间】:2016-07-29 14:16:36
【问题描述】:

我正在使用 dompdf 从输入生成 PDF。 我使用的代码:

require_once (STYLESHEETPATH.'/dompdf/include/autoload.inc.php');
set_include_path(STYLESHEETPATH.'/dompdf');

require_once STYLESHEETPATH.'/dompdf/dompdf_config.inc.php';

$dompdf = new DOMPDF();

$html = '
<html>
 <body>
  <h1>Hello <i>'. $fullname.'</i> </h1>
  <p> your father is <b>'.$fathername.'</b>
 </body>
</html>';

$dompdf->load_html($html);
$dompdf->render();

$dompdf->stream("tck.pdf"); ?>

这很好用并生成一个名为tck.pdf的pdf

现在我想为 pdf 中的字体设置样式,所以我尝试过

require_once (STYLESHEETPATH.'/dompdf/include/autoload.inc.php');
set_include_path(STYLESHEETPATH.'/dompdf');

require_once STYLESHEETPATH.'/dompdf/dompdf_config.inc.php';

$dompdf = new DOMPDF();

$html = '
<html>
    <head>
        <style>
            @font-face {
              font-family: "Open Sans";
              font-style: normal;
              font-weight: 400;
              src: url(http://themes.googleusercontent.com/static/fonts/opensans/v8/cJZKeOuBrn4kERxqtaUH3aCWcynf_cDxXwCLxiixG1c.ttf) format("truetype");
            }
        </style>
    </head>
 <body>
  <h1>Hello <i>'. $fullname.'</i> </h1>
  <p> your father is <b>'.$fathername.'</b>
 <br/>
  <span style="font-family:webfontregular;">bar code will be here</span>
    <div style="font-family:"Open Sans";">Open Sans</div>
 </body>
</html>';

$dompdf->load_html($html);
$dompdf->render();

$dompdf->stream("tck.pdf"); ?>

但它不起作用并给出如下错误:

Fatal error: Class 'Font' not found in /home4/q0i4l0g2/public_html/tck/wp-content/themes/kallyas-child/dompdf/include/font_metrics.cls.php on line 356

我注意到,每当我使用“@font-face”时,我都会收到此错误。 这是为什么? 我怎样才能让它发挥作用?

根据这个问题Dompdf and set different font-family 中接受的答案,我认为这应该有效,但它没有。

【问题讨论】:

  • @Chris 不,不是......如果这些问题的答案解决了这个问题,那么这个问题就没有机会获得 4 次投票......
  • 所以你是说你尝试过降级 php-font-lib?

标签: php css pdf fonts dompdf


【解决方案1】:

使用 0.7.0 之前的 dompdf 版本时,不应直接包含自动加载器。这是在包含 dompdf_config.inc.php 文件时执行的设置过程的一部分。该设置过程定义了 dompdf 使用的许多常量,并引用了 dompdf 的 autloaders 和使用的各种库(包括 php-font-lib)。

如果 dompdf 类已经可用,则启动脚本将退出而不做任何事情。

您如何获得 dompdf 也可能相关。如果您没有下载打包版本(即您分别下载了各种库),请确保您获取 php-font-lib 0.3.x,这是 dompdf 0.6 版本使用的版本。

【讨论】:

    猜你喜欢
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 2019-12-20
    • 2023-01-01
    • 1970-01-01
    • 2018-07-11
    相关资源
    最近更新 更多