Laravel Dompdf Add Custom Font Family Example

By Hardik Savani April 16, 2024 Category : Laravel

Hey,

Here, I will show you how to work laravel dompdf custom font. I explained simply step by step laravel dompdf custom font family. This article goes in detailed on how to add custom font in laravel dompdf. you will learn dompdf custom font laravel.

In this example, I'll guide you through the steps of customizing the font style in a PDF document using the Laravel Dompdf library. We'll explore how to download Google fonts and integrate them into your PDF file using Dompdf. To accomplish this, we'll employ the @font-face rule to define the font family for Dompdf.

Now, let's see example step by step, you can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions:

Step 1: Install Laravel

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Install DomPDF Package

next, we will install DomPDF package using following composer command, let's run bellow command:

composer require barryvdh/laravel-dompdf

Step 3: Download Custom Fonts

in this step, we will download new custom font from google fonts. so, let's click the following link:

Google Fonts

Google Croissant One Font

You need to just download it.

Now create new folder fonts in storage folder. Then just extract new font zip file as like the screenshot:

storage/fonts

Step 4: Create Controller

In this step, we will create PDFController with generatePDF() where we write code of generate pdf. so let's create controller using bellow command.

php artisan make:controller PDFController

Now, update the code on the controller file.

app/Http/Controllers/PDFController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use PDF;

class PDFController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function generatePDF()

{

$data = [

'title' => 'Welcome to ItSolutionStuff.com',

];

$pdf = PDF::loadView('myPDF', $data);

return $pdf->download('itsolutionstuff.pdf');

}

}

Step 5: Add Route

Furthermore, open routes/web.php file and update code on it.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\PDFController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('generate-pdf', [PDFController::class, 'generatePDF']);

Step 6: Create View File

In Last step, let's create myPDF.blade.php(resources/views/myPDF.blade.php) for layout of pdf file and put following code:

resources/views/myPDF.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel Dompdf Add Custom Font Family Example - ItSolutionStuff.com</title>

<style>

@font-face {

font-family: 'Croissant One';

font-weight: normal;

font-style: normal;

font-variant: normal;

src: url("fonts/Croissant_One/CroissantOne-Regular.ttf") format('truetype');

}

body {

font-family: 'Croissant One', sans-serif;

}

</style>

</head>

<body>

<p>Dear Boss,

<br/><br/>

I am writing to formally resign from my position as [Your Position] at [IT Company Name], with my last working day being [Last Working Day], in accordance with the notice period stipulated in my employment contract.

<br/><br/>

I want to express my sincere gratitude for the opportunities and experiences I've had during my time at [IT Company Name]. It has been an incredible journey, and I have had the privilege of working alongside an outstanding team of professionals.

<br/><br/>

After careful consideration, I have decided to take a new direction in my career. This decision wasn't easy, as I have cherished my time here and the projects we've accomplished together. I am immensely proud of our collective achievements.

<br/><br/>

During my notice period, I am committed to ensuring a seamless transition. I am more than willing to assist in the transfer of my responsibilities, provide training to my successor, and complete any pending projects. Please let me know how I can best contribute to this process.

<br/><br/>

Sincerely,<br/>

Hardik Savani

</p>

</body>

</html>

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/generate-pdf

you will download file as like below:

Now we are ready to run this example and check it...

I hope it can help you...

Tags :
Shares