To add a custom font to laravel-snappy, you need to follow these steps:
- Place the font file in the storage/fonts directory.
- In your controller, set the font path using the setOption method.
- 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.