Hello developers, In this example, I will explain to you how to do Laravel 5.6 PDF Generate Tutorial With Example. Here we use laravel 5.6, but you can also use in laravel 5.6 lesser version. We will use laravel-dompdf package for generating pdf in laravel application. In ERP system mostly used pdf for generating an invoice. If you require to get more information about the generated pdf, then go to Github
Laravel 5.6 PDF Generate Tutorial With Example
We are going to Configure Laravel Project.
#1: Download Laravel Project
Establish Laravel Project by the typing following command.
composer create-project --prefer-dist laravel/laravel pdfgenerate
#2: Configure SQL Database
Now we can setup database credentials.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=fifaworldcup DB_USERNAME=root DB_PASSWORD=
#3: Install laravel-dompdf package
First, install the laravel-dompdf package via the Composer package manager.
composer require barryvdh/laravel-dompdf
Let’s Add ServiceProvider in config/app.php
'providers' => [ .... Barryvdh\DomPDF\ServiceProvider::class, ],
And finally, add in the alias section config/app.php
'aliases' => [ .... 'PDF' => Barryvdh\DomPDF\Facade::class, ],
#4: Build Model and Migration File
php artisan make:model Footballerdetail -m
It will create a Footballerdetail.php file and also create a create_footballerdetails_table.php migration file.
// create_footballerdetails_table public function up() { Schema::create('footballerdetails', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('country'); $table->string('position'); $table->integer('goalscore'); $table->timestamps(); }); }
Save and run.
php artisan migrate
#5: Create a View File
Create a file in resources/views/footballerdetail.blade.php and put this following code in it.
<!-- footballerdetail.blade.php --> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Laravel 5.6 PDF Generate Tutorial With Example</title> <link rel="stylesheet" href="{{asset('css/app.css')}}"> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Laravel 5.6 PDF Generate Tutorial With Example</h2><br/> <form method="post" action="{{url('/footballerdetail/store')}}"> @csrf <div class="form-group row"> <label for="name" class="col-md-4 col-form-label text-md-right">Name:</label> <div class="col-md-2"> <input id="name" type="text" class="form-control" name="name"> </div> </div> <div class="form-group row"> <label for="country" class="col-md-4 col-form-label text-md-right">Country:</label> <div class="col-md-2"> <input id="country" type="text" class="form-control" name="country"> </div> </div> <div class="form-group row"> <label for="position" class="col-md-4 col-form-label text-md-right">Position:</label> <div class="col-md-2"> <input id="position" type="text" class="form-control" name="position"> </div> </div> <div class="form-group row"> <label for="goalscore" class="col-md-4 col-form-label text-md-right">Goal Score:</label> <div class="col-md-2"> <input id="goalscore" type="text" class="form-control" name="goalscore"> </div> </div> <div class=" form-group row"> <div class="col-md-4"></div> <div class="col-md-2"> <button type="submit" class="btn btn-success">Submit</button> </div> </div> </form> </div> </body> </html>
#6: Create one controller
php artisan make:controller FootballerdetailController
It will build a controller file called FootballerdetailController.php.
//FootballerdetailController.php public function create() { return view('footballerdetail'); }
#7: Define Route
We register all route in a web.php file.
Route::get('/footballerdetail','FootballerdetailController@create'); Route::post('/footballerdetail/store','FootballerdetailController@store'); Route::get('/footballerdetail/index','FootballerdetailController@index'); Route::get('/footballerdetail/downloadPDF/{id}','FootballerdetailController@downloadPDF');
#8: Store data to the database
Next, we are ready to fill in our store () method with the logic to store data to the database.
//FootballerdetailController.php use App\Footballerdetail; public function store(Request $request) { $footballer = new Footballerdetail(); $footballer->name = $request->get('name'); $footballer->country = $request->get('country'); $footballer->position = $request->get('position'); $footballer->goalscore = $request->get('goalscore'); $footballer->save(); return redirect('/footballerdetail/index'); }
Pass data to the view file so first; we create an index() function in Controller to pass data to view the file.
//FootballerdetailController.php public function index() { $footballers = Footballerdetail::all(); return view('index', compact('footballers')); }
# 9: Create a view file for the show the data.
Create a file in resources/views/index.blade.php and put this following code in it.
<!-- index.blade.php --> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" href="{{asset('css/app.css')}}"> </head> <body> <div class="container"> <table class="table table-striped"> <thead> <th>ID</th> <th>Name</th> <th>Country</th> <th>Position</th> <th>Goal Score</th> <th>Action</th> </thead> <tbody> @foreach($footballers as $footballer) <tr> <td>{{$footballer->id}}</td> <td>{{$footballer->name}}</td> <td>{{$footballer->country}}</td> <td>{{$footballer->position}}</td> <td>{{$footballer->goalscore}}</td> <td><a href="{{action('FootballerdetailController@downloadPDF', $footballer->id)}}">PDF</a></td> </tr> @endforeach </tbody> </table> </div> </body> </html>
#10: Create a view file to design our pdf blade
Create a file in resources/views/pdf.blade.php and put this following code in it.
<!-- pdf.blade.php --> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <table class="table table-bordered"> <tr> <td> {{$footballer->name}} </td> <td> {{$footballer->country}} </td> <td> {{$footballer->position}} </td> <td> {{$footballer->goalscore}} </td> </tr> </table> </body> </html>
#11: Write a function in the controller to download the PDF
//FootballerdetailController.php <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Footballerdetail; use PDF; class FootballerdetailController extends Controller { public function create() { return view('footballerdetail'); } public function store(Request $request) { $footballer = new Footballerdetail(); $footballer->name = $request->get('name'); $footballer->country = $request->get('country'); $footballer->position = $request->get('position'); $footballer->goalscore = $request->get('goalscore'); $footballer->save(); return redirect('/footballerdetail/index'); } public function index() { $footballers = Footballerdetail::all(); return view('index', compact('footballers')); } public function downloadPDF($id) { $footballer = Footballerdetail::find($id); $pdf = PDF::loadView('pdf', compact('footballer')); return $pdf->download('footballerdetail.pdf'); } }
At last, our Laravel 5.6 PDF Generate Tutorial With Example is over.

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of cloud technologies including Google Cloud, Firebase, AWS, and Azure, and various softwares and tools.