Laravel 8 Generate PDF File Using DomPDF


In this tutorial, I will tell you how to generate pdf using dompdf in laravel 8 application. I will share how to generate pdf in PHP with laravel 8. We will use dompdf(barryvdh/laravel-dompdf package) to generate a pdf file laravel 8 project. I write tutorials step by step to HTML design to generate pdf in laravel 8 applications.

PDF is one of the basic requirements when you are working on the eCommerce website. we need to create a pdf file for the invoice etc. So, here I will show you a very simple example to create a pdf file with laravel 8.

In this example, we will install barryvdh/laravel-dompdf composer package, and then we will add a new route with a new controller file. Then we will create one example blade file. Then after you have to just run the project and see the pdf file for download. You need to just follow a few steps and get a basic example of a pdf file.

[Laravel 8 PDF] Laravel 8 Generate PDF File Using DomPDF- Step By Step
Step 1 : Install Laravel 8

we need to get a fresh laravel 8 version application So let's open the terminal and run the below command to install a fresh laravel 8 project.

composer create-project --prefer-dist laravel/laravel blog
Step 2 : Install Dompdf Package

We need DomPDF package for HTML design to generate pdf So let's open terminal run bellow command install DomPDF package:

composer require barryvdh/laravel-dompdf


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

config/app.php

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

Now step create two routes for one route is preview pdf and second route to generate pdf and download pdf file. So let's open web.php file put bellow route:

routes/web.php

use App\Http\Controllers\PDFController;

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

Now create a new controller as PDFController and add two methods in this controller, the first method is a preview and the last one is to generate pdf and download pdf. So let's open the terminal and put the bellow command to create a controller file.

php artisan make:controller PDFController


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

app/Http/Controllers/PDFController.php

<?php

namespace App\Http\Controllers;
use PDF;

class PDFController extends Controller
{
    // function to display preview
    public function preview()
    {
        return view('preview');
    }

    public function generatePDF()
    {
        $pdf = PDF::loadView('preview');    
        return $pdf->download('demo.pdf');
    }
}
Step 5 : Create Blade File

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

resources/views/preview.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Generate PDF Laravel 8 - phpcodingstuff.com</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<style type="text/css">
    h2{
        text-align: center;
        font-size:22px;
        margin-bottom:50px;
    }
    body{
        background:#f2f2f2;
    }
    .section{
        margin-top:30px;
        padding:50px;
        background:#fff;
    }
    .pdf-btn{
        margin-top:30px;
    }
</style>    
<body>
    <div class="container">
        <div class="col-md-8 section offset-md-2">
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <h2>Laravel 8 Generate PDF - phpcodingstuff.com</h2>
                </div>
                <div class="panel-body">
                    <div class="main-div">
                            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
                        consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
                        cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
                        proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                        </div>
                </div>
                <div class="text-center pdf-btn">
                  <a href="{{ route('pdf.generate') }}" class="btn btn-primary">Generate PDF</a>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

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

php artisan serve


Now you can open bellow URL on your browser:

localhost:8000/pdf/preview

[Laravel 8 PDF] Laravel 8 Generate PDF File Using DomPDF- Step By Step

I hope it can help you...

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!

close