Normally, if we work on a big ERP level project on laravel, we require to generate PDF files for data from the database table. In this tutorial, I give you a very simple way to create a pdf file and give it to download that pdf step by step for beginners.
In this post, I use laravel-dompdf package for creating pdf file and this package also provides download function. In this example I have one table "items" and I will download pdf file of items table data with the tabular format, You can also write your own CSS on view file for pdf, so you can make a batter pdf file.
So, finally, you have to just follow a few steps and get pdf to generate and download, first you have a fresh project of Laravel 5 or Laravel 5.3, etc with work.
Step 1: Installation
In the first step, we have to download laravel-dompdf plugin to generate a pdf file from view blade file. So first run bellow command in your terminal:
composer require barryvdh/laravel-dompdf
Now open config/app.php file and add service provider and alias.
'providers' => [....Barryvdh\DomPDF\ServiceProvider::class,],'aliases' => [....'PDF' => Barryvdh\DomPDF\Facade::class,],
Step 2: Add Route
This is a step we need to add a route to generate the view. so open your app/Http/routes.php file and add the following route.
Route::get('pdfview','ItemController@pdfview')->name('PDF_report');
Read Also: Laravel 5 import export to excel and CSV using maatwebsite example.
Step 3: Create Controller
Ok, now we should create a new controller as ItemController in this path app/Http/Controllers/ItemController.php. Make sure you should have items table with some data. this controller will manage data and generate a pdf file, so put bellow content in the controller file:
app/Http/Controllers/ItemController.php
Step 4: Create View File<?php namespace App\Http\Controllers; use App\Http\Requests; use Illuminate\Http\Request; use DB; use PDF; class ItemController extends Controller { /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function pdfview() { $pdfView = DB::select('SELECT * FROM users'); $pdf = PDF::loadView('pdfview', ['pdfViews'=> $pdfView]) ->setPaper('a4','landscape'); return $pdf->download('report.pdf') } }
In the last step, we have to create a view file "pdfview.blade.php" to generate the view and also pdf file, so create a pdf view file and put bellow code:
resources/view/pdfview.blade.php
Read Also: Laravel 5.7 - Generate PDF from HTML Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <style type="text/css"> table td, table th { border:1px solid black; } </style> <div class="container"> <br/> a class="btn btn-warning" href="{{ route('PDF_report') }}">Download PDF</a> <table> <tr> <th>id</th> <th>name</th> <th>email</th> <th>email_verified_at</th> <th>created_at</th> <th>created_at</th> </tr> @foreach ($pdfViews as $pdfView) <tr> <td>{{$pdfView->id }}</td> <td>{{ $pdfView->name }}</td> <td>{{ $pdfView->email }}</td> <td>{{ $pdfView->email_verified_at }}</td> <td>{{ $pdfView->created_at }}</td> <td>{{ $pdfView->created_at }}</td> </tr> @endforeach </table> </div> </body> </html>
$ php artisan serve
http://127.0.0.1:8000/pdfview
0 Comments
CAN FEEDBACK
Emoji