How to create html template and generate PDF in laravel 5.8

By Super Admin | Jan 13, 2021 | Laravel
Share :

Laravel 5.8 - How to Generate HTML to PDF with Laravel domPDF

In this post, we are going to learn about how to generate PDF in laravel 5.8 where i am using a simple html template to convert into PDF in laravel 5.8.

To generate HTML to PDF with laravel-domPDF, I am using a package called barryvdh/laravel-dompdf 

So, Let's get started:

Step 1: Download or install the laravel 5.8 version using the following command:

$ composer create-project --prefer-dist laravel/laravel blog "5.8.*"

Step 2: Now, we have to install the package laravel-dompdf Package with the following command:

$ composer require barryvdh/laravel-dompdf

After installing the laravel-dompdf package successfully, setup with below steps. 

Step 3: Open the app.php file in the following path: config/app.php. Add service provider and alias as shown below:

'providers' => [


'aliases' => [

    'PDF' => Barryvdh\DomPDF\Facade::class,

Step 4: Create a Controller named as PdfgenerateController using the following command: 

$ php artisan make:controller PdfgenerateController

After creating the controller successfully, paste the below code in the PdfgenerateController.php


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;

class PdfgenerateController extends Controller
    public function generatePDF()
        $data = [
            'heading' => 'Welcome to Funda of Web IT',
            'description' => 'This description of Funda of Web IT'
        $pdf = PDF::loadView('yourPDF'$data);

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



Step 5: Let's create a route to generate the pdf and connect with the above created Controller as shown below:


Step 6: Final step, now we will create a file named yourPDF.blade.php in the following path: resource/views/yourPDF.blade.php and put the following code:

<!DOCTYPE html>
    <title>Funda of Web IT</title>
    <h1>Hey, {{ $heading }}</h1>
    <h5>{{ $description }}</h5>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo

Thanks for reading..

Share this blog on social platforms