Laravel mPdf add custom font

  1. Download the font you want to use and save it in your Laravel project's "public/fonts" directory.

  2. Open your controller or create a new one and import the mPDF class at the top of your file:


use Mpdf\Mpdf;

Create a new instance of the mPDF class:


$pdf = new Mpdf();

Use the "AddFont" method to add the font to mPDF. You'll need to provide the path to the font file, the font name, and any additional options you want to set. Here's an example


$font_path = public_path('fonts/YourFontName.ttf');

$pdf->AddFont('YourFontName', '', $font_path);



Replace "YourFontName" with the actual name of the font file (without the ".ttf" extension).

You can also set additional options such as the font weight and style



$pdf->AddFont('YourFontName', 'B', $font_path, 32);


  1. In this example, the font weight is set to "B" (bold) and the font size is set to 32.

  2. Use the "SetFont" method to set the font you just added as the default font:



$pdf->SetFont('YourFontName', '', 12);


  1. In this example, the font size is set to 12.

  2. Add content to your PDF as usual using the mPDF class.

  3. Generate the PDF and return it as a download or stream:


$pdf->Output('YourFileName.pdf', 'D');


  1. Replace "YourFileName.pdf" with the name you want to give your PDF file, and "D" to download the file.

That's it! You have successfully added a custom font to mPDF in your Laravel project.



No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...