在Laravel DOMPDF 0.8.3 / Laravel 5.8中导入自定义字体

时间:2019-05-16 11:05:37

标签: dompdf custom-font laravel-5.8

我尝试在Laravel Dompdf中导入我的自定义字体VAGRounded_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。 我怎么能达到我的目标?

谢谢。

1 个答案:

答案 0 :(得分:0)

您需要在@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元素上使用,因为它并未注册用于粗体文本。