dompdf未生成.ufm和dompdf_font-family_cache.php文件

发布于 2025-01-19 18:41:09 字数 1333 浏览 1 评论 0原文

我试图在生成 pdf 文件时在 dompdf 中使用自定义字体。我发现自定义字体仅在生成 .ufm 和缓存文件后才有效。

Drupal php 文件

public function GeneratePdf() {
  $dompdf = new Dompdf();
  $fontDirectory = $host.'/'.$themepath  . '/fonts';
  $options->setChroot($fontDirectory);
  $dompdf->setOptions($options);
  ob_end_clean();
  $dompdf->getFontMetrics()->registerFont(
    ['family' => 'montserrat', 'style' => 'normal', 'weight' => 'normal'],
    $fontDirectory  . '/Montserrat-Regular.ttf'
  );
  $dompdf->loadHtml($html);
  $dompdf->render();
  ob_end_clean();
}

Pdf 生成 HTML .twig 文件。

<html>
  <head>
    <style type="text/css">
      @page {
        size: auto;
        margin: 0;
        padding: 0;
      }      
     @font-face {
        font-family: montserrat;
        src: url($themepath ~'/fonts/Montserrat-Regular.ttf') format('truetype'); font-style: normal;
      }
    </style>
  </head>
  <body style="margin:0; padding:0;">   
    <span style="font-family:'montserrat';color:#417bac; font-size:18px">PDF font testing</span>
  </body>
</html>

问题是通过 php 函数注册字体后所需的文件,即 .ufm 和 dompdf_font-family_cache.php 未在 dompdf 文件夹中创建 >供应商\dompdf\dompdf\lib\字体

I am trying to use custom font in dompdf while generating pdf file. As i found that custom font works only after generating .ufm and cache file.

Drupal php file

public function GeneratePdf() {
  $dompdf = new Dompdf();
  $fontDirectory = $host.'/'.$themepath  . '/fonts';
  $options->setChroot($fontDirectory);
  $dompdf->setOptions($options);
  ob_end_clean();
  $dompdf->getFontMetrics()->registerFont(
    ['family' => 'montserrat', 'style' => 'normal', 'weight' => 'normal'],
    $fontDirectory  . '/Montserrat-Regular.ttf'
  );
  $dompdf->loadHtml($html);
  $dompdf->render();
  ob_end_clean();
}

Pdf generation HTML .twig file.

<html>
  <head>
    <style type="text/css">
      @page {
        size: auto;
        margin: 0;
        padding: 0;
      }      
     @font-face {
        font-family: montserrat;
        src: url($themepath ~'/fonts/Montserrat-Regular.ttf') format('truetype'); font-style: normal;
      }
    </style>
  </head>
  <body style="margin:0; padding:0;">   
    <span style="font-family:'montserrat';color:#417bac; font-size:18px">PDF font testing</span>
  </body>
</html>

Issue is after registering font through php function required files i.e .ufm and dompdf_font-family_cache.php not created in dompdf folder vendor\dompdf\dompdf\lib\fonts

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文