Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

How to add a custom font to laravel-snappy?

Hello,

I've set up laravel-snappy and it's working, but I can't get my custom font to work. It's a special font for RTL language.

I put the font in /storage/fonts/MyCustomFont.ttf, and then I tried to set it up:

$pdf = App:make('snappy.pdf.wrapper');
$pdf = PDF::loadHtml('<h1>My Custom Font Test</hi>');
$pdf->setOption('encoding', 'UTF-8');
$pdf->setOption('no-collate', true);
$pdf->setOption('header-font-name', 'MyCustomFont');
return $pdf->download('test.pdf');

But it doesn't show the custom font, but some other font instead.

Why? I placed the ttf file with the same name in the right folder. What did I miss?

Thanks

0 likes
2 replies
LaryAI's avatar
Level 58

To add a custom font to laravel-snappy, you need to follow these steps:

  1. Place the font file in the storage/fonts directory.
  2. In your controller, set the font path using the setOption method.
  3. Use the font in your HTML/CSS.

Here's an example:

use PDF;

public function generatePdf()
{
    $pdf = PDF::loadView('pdf.template');

    $fontPath = storage_path('fonts/MyCustomFont.ttf');
    $pdf->setOption('user-style-sheet', "body { font-family: 'MyCustomFont'; }");
    $pdf->setOption('header-font-name', 'MyCustomFont');
    $pdf->setOption('header-font-size', '14');
    $pdf->setOption('header-spacing', '5');
    $pdf->setOption('margin-top', '30');
    $pdf->setOption('margin-bottom', '30');
    $pdf->setOption('margin-left', '20');
    $pdf->setOption('margin-right', '20');
    $pdf->setOption('encoding', 'UTF-8');
    $pdf->setOption('no-collate', true);
    $pdf->setOption('enable-javascript', true);
    $pdf->setOption('javascript-delay', 1000);
    $pdf->setOption('no-stop-slow-scripts', true);
    $pdf->setOption('dpi', 300);
    $pdf->setOption('image-dpi', 300);
    $pdf->setOption('image-quality', 100);
    $pdf->setOption('enable-smart-shrinking', true);
    $pdf->setOption('use-xserver', true);
    $pdf->setOption('font-path', $fontPath);

    return $pdf->stream('document.pdf');
}

In this example, we're setting the font path using the setOption method, and then using the font in the HTML/CSS using the user-style-sheet option.

fefo-p's avatar

How can I add a horizontal bar before each footer in snappy-laravel?

Please or to participate in this conversation.