Latest Post


Laravel 8 Generate PDF Example Tutorial

Hi Guys,

In this tutorial, I will learn you how to generate pdf using dompdf in laravel 8 application. I will share with you how to create pdf file from html design in laravel 8. We will use dompdf(barryvdh/laravel-dompdf package) for generate pdf file with laravel 8 project. i write tutorial step by step to html design to generate pdf in laravel 8 application.

PDF is one of rudimentary requisite when you are working with erp level project or ecommerce website. we may need to engender pdf file for report or invoice etc. So, here i will give you very simple example for engender pdf file with laravel 8.

in this example, we will install barryvdh/laravel-dompdf composer package and then after we will integrate incipient route with incipient controller file. Then we will engender one example blade file. Then after you have to just run project and visually perceive pdf file for download. You require to just follow few steps and get rudimental example of pdf file.

Step 1 : Install Laravel 8

In the first step, we require to get fresh laravel 8 version application So let's open terminal and run bellow command to install fresh laravel project.

composer create-project --prefer-dist laravel/laravel blog
Step 2 : Install dompdf package

We require dompdf package for html design to engender pdf So let's open terminal run bellow command to install dompdf package:

composer require barryvdh/laravel-dompdf

After successfully install package, open config/app.php file and add service provider and alias.

'providers' => [
'aliases' => [
    'PDF' => Barryvdh\DomPDF\Facade::class,
Step 3 : Add Route

In this step, We have to engender two route for one route is preview pdf and second route to engender pdf and download pdf file. So let's open web.php file put bellow route:

use App\Http\Controllers\DompdfController;

Route::get('pdf-preview', [DompdfController::class, 'preview'])->name('pdf.preview');
Route::get('pdf-generate', [DompdfController::class, 'generatePDF'])->name('pdf.generate');
Step 4 : Create Controller

Here you can create new controller as DompdfController and add two method in this controller, first method is preview and last one is generate pdf and download pdf. So let's open terminal and put bellow command to create controller file.

php artisan make:controller DompdfController

Now we can add two methods in DompdfController file So let's open DompdfController.php file put bellow code.


namespace App\Http\Controllers;
use PDF;

class DompdfController extends Controller
    // function to display preview
    public function preview()
        return view('pdf');

    public function generatePDF()
        $pdf = PDF::loadView('pdf');    
        return $pdf->download('demo.pdf');
Step 5 : Create blade file

In last step. In this step we have to create preview blade file. So mainly we have to create preview file. So finally you have to create one preview blade file for preview pdf design:

<!DOCTYPE html>
    <title>Generate PDF Laravel 8 - CodingTracker</title>
    <link rel="stylesheet" href="">
<style type="text/css">
        text-align: center;
<body class="bg-dark">
    <div class="container">
        <div class="col-md-8 section offset-md-2">
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <h2 class="bg-secondary text-white">Laravel 8 Generate PDF Example Tutorial - CodingTracker</h2>
                <div class="panel-body">
                    <div class="main-div">
                            CodingTracker Blog provides you latest coding related Tutorials on PHP, Laravel, Codeigniter, JQuery,HTML,CSS, Node js, React js, Vue js, PHP, and Javascript and another gadget.
                <div class="text-center pdf-btn">
                  <a href="{{ route('pdf.generate') }}" class="btn btn-success">Generate PDF</a>

Now we are ready to run our crud application example with laravel 8 so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:


You will see layout as like bellow:

Preview PDF Design:

It will help you...

Post a Comment