n Laravel 8 Generate PDF File Using DomPDF | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

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.

 

Install dompdf Package

first of all we will install barryvdh/laravel-dompdf composer package by following composer command in your laravel 8 application.

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,

]

 

Add Route

In this is step we need to create routes for items listing. so open your "routes/web.php" file and add following route.

routes/web.php

 Route::get('generatepdf', [PDFController::class, 'generatePDF']);

 

Add Controller

Here,we require to create new controller PDFController that will manage generatePDF method of route. So let's put bellow code.

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 dompdf',
'body' => 'Your description',

];


$pdf = PDF::loadView('products.show', $data);

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

}

}

 

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/products/show.blade.php

 

<!DOCTYPE html>
<html>
<head>
    <title>Generate PDF Laravel 8</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>{{ $title }}</h2>
                </div>
                <div class="panel-body">
                    <div class="main-div">
                          {{ $body }}
                        </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

 

 

I hope you found this article useful. let me know if you have any questions and I’ll be happy to answer them.

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook