【问题标题】:Import custom-font in Laravel DOMPDF 0.8.3 / Laravel 5.8在 Laravel DOMPDF 0.8.3 / Laravel 5.8 中导入自定义字体
【发布时间】:2019-05-16 11:05:36
【问题描述】:

我尝试在 Laravel Dompdf 中导入我的自定义字体 VAGRooned_BT.ttf。我已将 config/dompdf.php 中的目录以及 font_cache 更改为 public/fonts/。但我无法在我的 PDF 中显示字体。

互联网上提供的几种解决方案对我没有帮助。我找不到问题,分别是如何导入我的字体。我尝试了不同的字体,比如 Lato 或 Roboto,但总是一样。

config/dompdf.php 的变化

        "font_dir" => storage_path('public/fonts/'), 

        "font_cache" => storage_path('public/fonts/'),

控制器功能:

    public function deliveryDetailsPDF($delivery_id){

        $header   = Delivery::where('id',$delivery_id)->first();
        $details  = Delivery::getDeliveryDetails($delivery_id);

        $pdf = \PDF::setOptions([
                      'isHtml5ParserEnabled'=> true,
                      'isRemoteEnabled' => true,
                    ])
                    ->loadView('delivery.pdf',compact('header','details'))
                    ->stream();


        return $pdf;
    }

Blade文件delivery/pdf.blade.php头部中的样式块:

  <style>
    @font-face{
        font-family: VAGBlack;
        src:url('/fonts/VAGRounded_BT.ttf');
        /* src:url({{ storage_path('fonts\VAGRounded_BT.ttf') }}) format("truetype"); */
    }


    h1, h2 {
      font-family:VAGBlack;
      font-size: 14pt;
      color:black;
    }
    </style>

在生成的 PDF 中仍然使用 Arial。 我怎样才能达到我的目标?

提前谢谢你。

【问题讨论】:

    标签: dompdf custom-font laravel-5.8


    【解决方案1】:

    您需要在@font-face 声明中更加明确。请尝试以下操作:

    @font-face{
        font-family: VAGBlack;
        src: url('/fonts/VAGRounded_BT.ttf') format('truetype');
        font-weight: bold;
        font-style: normal;
    }
    

    我指定了粗体字重,因为这就是 h1 和 h2 元素的样式。如果不指定权重和样式:

    • 我不确定 Dompdf 是否会注册字体。
    • 即使 Dompdf 确实注册了字体,它也不会在 H1 和 H2 元素上使用它,因为它没有注册用于粗体文本。

    【讨论】:

      猜你喜欢
      • 2020-06-12
      • 1970-01-01
      • 2021-10-23
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-22
      • 1970-01-01
      相关资源
      最近更新 更多