Laravel 6 pdf generator tutorial using dompdf
In this article, we will discuss how to generate a pdf file from the blade view or HTML view in Laravel using laravel-dompdf package. There are many packages available but dompdf package through we can easily generate the pdf file.
Why we need to pdf file? when we need to generate the invoice, booking ticket generate, large data convert into pdf invoice and many more things. in case we need to pdf file.
There are many packages available but dompdf package through we can easily generate the pdf file.
Overview
Step 1: Install Laravel
Step 2: Setting Database Configuration
Step 3: Install laravel-dompdf Package
Step 4: Add providers and aliases
Step 5: Create Route
Step 6: Create Model and Controller
Step 7: Create Blade Files
Step 1 : Install Laravel
We are going to install laravel 6, so first open the command prompt or terminal and go to go to xampp htdocs folder directory using the command prompt. after then run the below command.
1 | composer create-project --prefer-dist laravel/laravel laravel_dompdf |
Step 2: Setting Database Configuration
After complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.
1 2 3 4 5 6 | DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name(laravel_dompdf) DB_USERNAME=Enter_Your_Database_Username(root) DB_PASSWORD=Enter_Your_Database_Password(root) |
Step 3: Install laravel-dompdf Package
Now, We will install laravel-dompdf package using below command.
1 | composer require barryvdh/laravel-dompdf |
Step 4: Add providers and aliases
We will add below providers and aliases in the “config/app.php” file.
1 2 3 4 5 6 7 8 | 'providers' => [ .... Barryvdh\DomPDF\ServiceProvider::class, ], 'aliases' => [ .... 'PDF' => Barryvdh\DomPDF\Facade::class, ] |
Step 5: Create Route
Add the below following route code in the “routes/web.php” file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php /* |-------------------------------------------------------------------------- | 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('/', function () { // return view('welcome'); }); Route::get('/student/pdf','StudentController@pdf'); ?> |
Step 6: Create Model and Controller
Here below command help to create the controller and model.
1 | php artisan make:controller StudentController --resource --model=Student |
Student.php
1 2 3 4 5 6 7 8 9 10 11 | <?php namespace App; use Illuminate\Database\Eloquent\Model; class Student extends Model { // protected $fillable = [ 'first_name','last_name', 'address' ]; } ?> |
StudentController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php namespace App\Http\Controllers; use App\Student; use Illuminate\Http\Request; use Response; use PDF; class StudentController extends Controller { public function pdf(Request $request) { $students = Student::all(); $pdf = PDF::loadView('student.list', $students); $pdf->save(storage_path().'_student.pdf'); return $pdf->download('student.pdf'); } } ?> |
Step 7: Create Blade Files
Finally, We will create list.blade.php file in the “resources/views/student/” folder directory and paste below code.
list.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <div class="row"> <a href="{{ URL::to('/student/pdf') }}">Export PDF</a> <table> <tr> <th>First Name</th> <th>Last Name</th> <th>Address</th> </tr> @foreach ($students as $student) <tr> <td>{{ $student->first_name }}</td> <td>{{ $student->last_name }}</td> <td>{{ $student->address }}</td> </tr> @endforeach </table> </div> |
We can start the server and run this example using the below command.
1 | php artisan serve |
Now we will run our example using the below Url in the browser.
1 | http://127.0.0.1:8000/student |