Laravel 9 Generate PDF Example Tutorial

10-Apr-2023

.

Admin

Laravel 9 Generate PDF Example Tutorial

Hi Dev,

Today, I will tell you how to generate pdf using dompdf in laravel 9 application. I will share with you how to create pdf file from html design in laravel 9. We will use dompdf(barryvdh/laravel-dompdf package) for generate pdf file with laravel 9 project. i write tutorial step by step to html design to generate pdf in laravel 9 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 9.

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: Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app

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

<?php

use App\Http\Controllers\PDFController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('pdf/preview', [PDFController::class, 'preview'])->name('pdf.preview');

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

Step 4: Add 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

{

/**

* Write code on Method

*

* @return response()

*/

public function preview()

{

return view('preview');

}

/**

* Write code on Method

*

* @return response()

*/

public function generatePDF()

{

$pdf = PDF::loadView('preview');

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

}

}

Step 5: Add 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 9 - 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 9 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>

Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

localhost:8000/pdf/preview

Output :

Preview PDF:

It will help you...

#Laravel 9