如何在laravel dompdf中自定义字体和页面?

时间:2017-05-01 14:49:42

标签: php laravel pdf laravel-5.3 dompdf

我从这里开始:https://github.com/barryvdh/laravel-dompdf

我的控制器是这样的:

public function listdata()
{
    $pdf=PDF::loadView('print_tests.test_pdf');
    $pdf->setPaper('L', 'landscape');
    return $pdf->stream('test_pdf.pdf');
}

我的观点是这样的:

<script type="text/php">

    if ( isset($pdf) ) {
        $x = 72;
        $y = 18;
        $text = "{PAGE_NUM} of {PAGE_COUNT}";
        $font = $fontMetrics->get_font("Arial", "bold");
        $size = 6;
        $color = array(0,0,0);
        $word_space = 0.0;  //  default
        $char_space = 0.0;  //  default
        $angle = 0.0;   //  default
        $pdf->page_text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle);
    }

</script>

我用这个:

  

&#34; barryvdh / laravel-dompdf&#34;:&#34; ^ 0.7.0&#34;,

执行时,字体不是Arial,页面不显示。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

尝试使用 laravel 5.5以上

1。首先下载要添加(或设置)为 .ttf 扩展名的字体

2.store存放在laravel存储文件夹中,例如storage/fonts/yourfont.ttf

3。在刀片内使用CSS @ font-face 规则。

4。例如,创建sample.blade.php文件

<html>
<head>
    <meta http-equiv="Content-Type" content="charset=utf-8" />
    <style type="text/css">
        @font-face {
            font-family: 'yourfont'; //you can set your custom name also here..
            src: url({{ storage_path('fonts/yourfont.ttf') }}) format('truetype');
            font-weight: 400; // use the matching font-weight here ( 100, 200, 300, 400, etc).
            font-style: normal; // use the matching font-style here
         }        
        body{
            font-family: "yourfont",  //set your font name u can set custom font name also which u set in @font-face css rule
    </style>
</head>
<body>
    Check your font is working or not...
</body>
</html>