To add a custom font to Mpdf in Laravel, you can follow these steps:
Download the font files: First, you need to download the font files in the appropriate format. Mpdf supports TrueType and OpenType fonts. You can download the font files from the font's official website or from a font repository like Google Fonts.
Store the font files: Store the font files in your Laravel application's public directory, under a subfolder named "fonts". You can create the "fonts" folder manually or by using Laravel's File facade, like so:
- Register the font with Mpdf: In your Laravel application, open the Mpdf configuration file, typically located at
config/mpdf.php
. In this file, add an entry to thefontdata
array that specifies the font's name, the path to the font file, and any other necessary parameters. For example:
This code specifies a font named "myfont", with four variants: regular, bold, italic, and bold italic. The font files are stored in the "fonts" directory. The useOTL
and useKashida
options are optional and control advanced font features.
- Use the font in Mpdf: In your Laravel code, when creating an Mpdf object, you can specify the font you want to use by passing its name to the
SetFont()
method. For example:
This code creates an Mpdf object with UTF-8 encoding and A4 page size, and sets the font to "myfont" in bold style, with a font size of 14pt.
That's it! With these steps, you can add a custom font to Mpdf in Laravel and use it in your PDF generation code.
No comments:
Post a Comment