Updated 12 September 2023
Generating product PDFs for promotion is widely used in the laravel eCommerce systems. In this article, we are going to see how to generate PDF in laravel.
Dompdf is an HTML to PDF converter The dompdf is (mostly) the CSS 2.1 compliant HTML layout and rendering engine written in PHP language.
It is the style-driven renderer: it will download and read an external stylesheets, inline style tags, and all the style attributes of individual HTML elements. It also supports most presentational HTML attributes.
With the help of a composer you can easily install the dependencies of laravel-dompdf package.
Simply run this command inside your laravel project:
1 |
composer require barryvdh/laravel-dompdf |
It will install all the dependencies of 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. |
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, ], |
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> |
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 mmigration 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 migrate command
1 |
php artisan migrate |
Ex: PDF contrtoller. So, run this command
1 |
php artisan make:controller PDF Controller -resources |
Now inside your web->routes.php, Create two routes for create and store the data
1 2 |
Route::get('disneyplus', 'DisneyplusController@create')->name('disneyplus.create'); Route::post('disneyplus', 'DisneyplusController@store')->name('disneyplus.store'); |
Create a form and get the data from the user and send it to the controller via he routes you have created already.
Store the data yiou 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']; } |
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 |
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 |
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 which will be generated inside 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 into PDF Format.
Source:
DOM PDF Github
Collaborate with Laravel Development Company to develop customized solutions for E-commerce, CRM, and third-party API integrations.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
2 comments
Kindly raise your issue at Forums our Developer will assist you there.
Link: https://forums.bagisto.com/
Thanks & Regards