-->

How can I customize font and page in laravel dompd

2019-07-30 14:53发布

问题:

I get from here : https://github.com/barryvdh/laravel-dompdf

My controller is like this :

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

My view is like this :

<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>

I use this :

"barryvdh/laravel-dompdf": "^0.7.0",

When executed, the font is not Arial and the page is not display.

How can I solve it?

回答1:

try in laravel 5.5 above.

1.first download the font which u want to add(or set) as .ttf extenstion

2.store inside laravel storage folder like this storage/fonts/yourfont.ttf

3.Use CSS @font-face rules inside your blade..

4.for example create sample.blade.php file like this

<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>