It allows businesses to share product details in a neat and printable way.
This makes it easier for customers, partners, and team members to understand the product information clearly.
These PDF files are often used for catalogs, invoices, quotes, and marketing materials in online shops.
They simplify the process of downloading, printing, and sharing important information quickly and effectively.
In this article, we will explore how to generate a PDF in Laravel step by step using easy methods and tools.
What is dompdf library?
Dompdf is an HTML to PDF converter. Dompdf is (mostly) the CSS 2.1-compliant HTML layout and rendering engine written in PHP.
It is the style-driven renderer that will download and read external stylesheets, inline style tags, and all the style attributes of individual HTML elements.
It also supports most presentational HTML attributes.
How to generate a PDF in Laravel?
Step 1: Install the laravel-dompdf package
With the help of a composer, you can easily install the dependencies of the Laravel-DomPDF package.
Simply run this command inside your Laravel project:
|
1 |
composer require barryvdh/laravel-dompdf |
It will install all the dependencies of the laravel-dompdf package in your Laravel project.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
➜ laravel6 git:(master) composer require barryvdh/laravel-dompdf Using version ^0.8.5 for barryvdh/laravel-dompdf ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 5 installs, 0 updates, 0 removals - Installing sabberworm/php-css-parser (8.3.0): Downloading (100%) - Installing phenx/php-svg-lib (v0.3.3): Downloading (100%) - Installing phenx/php-font-lib (0.5.1): Downloading (100%) - Installing dompdf/dompdf (v0.8.3): Downloading (100%) - Installing barryvdh/laravel-dompdf (v0.8.5): Downloading (100%) dompdf/dompdf suggests installing ext-imagick (Improves image processing performance) dompdf/dompdf suggests installing ext-gmagick (Improves image processing performance) Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: barryvdh/laravel-dompdf Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. |
Step 2: Configure the laravel-dompdf package
Just go to Config/App.php and create these entries in providers and aliases.
|
1 2 3 4 5 6 7 8 |
'providers' => [ .... Barryvdh\DomPDF\ServiceProvider::class, ], 'aliases' => [ .... 'PDF' => Barryvdh\DomPDF\Facade::class, ], |
Step 3: Create a blade file
Inside the resources → views folder, create a new file called layout.blade.php.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Laravel 6 CRUD Example</title> <link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" /> </head> <body> <div class="container"> @yield('content') </div> <script src="{{ asset('js/app.js') }}" type="text/js"></script> </body> </html> |
Step 4: Create Models and Migrations for it
To create the migration, run this command
|
1 |
php artisan make:migration create_pdf_data_table |
To create the Model, run this command
|
1 |
php artisan make:model PDFData |
Now go to the migration file, i.e., create_pdf_data_table, and add your columns.
|
1 2 3 4 5 6 7 8 9 10 |
public function up() { Schema::create('disneypluses', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('show_name'); $table->string('series'); $table->string('lead_actor'); $table->timestamps(); }); } |
Now run the migrate command
|
1 |
php artisan migrate |
Step 5: Create controllers and Routes
Example: PDF controller. So, run this command
|
1 |
php artisan make:controller PDF Controller -resources |
Now, inside your web → routes.php, create two routes for creating and storing the data
|
1 2 |
Route::get('disneyplus', 'DisneyplusController@create')->name('disneyplus.create'); Route::post('disneyplus', 'DisneyplusController@store')->name('disneyplus.store'); |
Step 6: Get the data from the user
Create a form, get the data from the user, and send it to the controller via the routes you have created already.
Step 7: Store your data in the database
Store the data you have fetched from the form and create an entry into the database:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\PDFGenerate; class PDFGenerateController extends Controller { public function create() { return view('form'); } public function store(Request $request) { $validatedData = $request->validate([ 'company_name' => 'required|max:255', 'department_name' => 'required|max:255', 'team_lead_name' => 'required|max:255', ]); PDFGenerate::create($validatedData); return redirect('/pdfgenerate')->with('success', 'Your PDF Data is successfully saved'); } } |
Also, add the fillable fields inside the PDFData.php model file.
|
1 2 3 4 5 6 7 8 9 10 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Disneyplus extends Model { protected $fillable = ['show_name', 'series', 'lead_actor']; } |
Step 8: View the data into a blade file
|
1 |
Route::get('pdfdata/list', 'PDFDataController@index')->name('disneyplus.index'); |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
@extends('layout') @section('content') <table class="table table-striped"> <thead> <th>ID</th> <th>Show Name</th> <th>Series</th> <th>Lead Actor</th> <th>Action</th> </thead> <tbody> @foreach($shows as $show) <tr> <td>{{$show->id}}</td> <td>{{$show->company_name}}</td> <td>{{$show->department_name}}</td> <td>{{$show->team_lead_name}}</td> </tr> @endforeach </tbody> </table> @endsection |
Step 9: Create a route to download the PDF file
Add the following code inside the route file.
|
1 |
Route::get('/downloadPDF/{id}','PDFDataController@downloadPDF'); |
Now, update the list.blade.php file and add the Download PDF link.
|
1 2 3 4 5 6 7 8 9 |
@foreach($shows as $show) <tr> <td>{{$show->id}}</td> <td>{{$show->company_name}}</td> <td>{{$show->department_name}}</td> <td>{{$show->team_lead_name}}</td> <td><a href="{{action('PDFDataController@downloadPDF', $show->id)}}">Download PDF</a></td> </tr> @endforeach |
Step 10: Create the pdf.blade.php file to design the PDF
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <table class="table table-bordered"> <thead> <tr> <td><b>Company Name</b></td> <td><b>Department Name</b></td> <td><b>Team Lead name</b></td> </tr> </thead> <tbody> <tr> <td> {{$show->company_name}} </td> <td> {{$show->department_name}} </td> <td> {{$show->team_lead_name}} </td> </tr> </tbody> </table> </body> </html> |
We have created a simple table that will be generated inside the PDF.
Step 11: Write a controller function to download the PDF
|
1 2 3 4 5 6 |
public function downloadPDF($id) { $show = Disneyplus::find($id); $pdf = PDF::loadView('pdf', compact('show')); return $pdf->download('disney.pdf'); } |
And this is it. You can now download your generated file in PDF Format.
Source:
DOM PDF Github
Collaborate with Laravel Development Company to develop customized solutions for E-commerce, CRM, and third-party API integrations.
Final Thoughts
Adding PDF generation features to a Laravel application makes document handling simpler and improves daily workflow.
It helps teams create and share important files in a clear and consistent format.
Libraries like Dompdf make it easy to convert HTML content into well-structured PDF documents without complex coding steps.
This helps developers save time and work more efficiently on web projects.