Laravel 8 Generate PDF Example Tutorial

Laravel , Laravel 8

Nicesnippets

93

15-09-2020


Hi Dev,

Today, I will tell 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 basic requirement when you are working with erp level project or ecommerce website. we may need to create pdf file for report or invoice etc. So, here i will give you very simple example for create pdf file with laravel 8.

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

Step 1 : Install Laravel 8

In the first step, we need 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 need dompdf package for html design to generate 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.

config/app.php

'providers' => [

....

Barryvdh\DomPDF\ServiceProvider::class,

],

'aliases' => [

....

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

]

Step 3 : Add Route

In this step, We have to create two route 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

Here you can create new controller as PDFController 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 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 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:

resources/views/preview.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Generate PDF Laravel 8 - NiceSnippets.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 - NiceSnippets.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.

<br>

<br>

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 quick run:

php artisan serve

Now you can open bellow URL on your browser:

localhost:8000/pdf/preview

You will see layout as like bellow:

Preview Design:

Preview PDF:

It will help you...


Recommended Posts