Today laravelcode share with you how to convert html to pdf in laravel and how to download pdf file which generate fron html layout. in many big laravel project like e-commerce, erp and industrial relations related project you required this type functionality in laravel.
In this tutorials we are using on of the best package barryvdh/laravel-dompdf it provide very easy way to generate html to pdf in laravel.
We are in this tutorials create one static html pdf with table. but you are create one also dynamic data. like you can get data from your database and also make some action on it. we are also explain in this tutorials how to padd data in pdf view it is very simple and stupid way.
We are explain with all code step by step.
Step : 1 Install package
First we need to install package in our laravel application using following command
composer require barryvdh/laravel-dompdf
Step : 2 Configure package
After install pdf package for laravel then we must be configure it. just following littel thing. oprn your config/app.php file and set following value for providers array and aliases array
'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
....
'PDF' => Barryvdh\DomPDF\Facade::class,
],
After configure app.php file then open your bootstrap/app.php file and add following line like that.
Please make sure add following line above the return $app;
$app->singleton(\Barryvdh\DomPDF\ServiceProvider::class);
After adding above line then run following command in your terminal/cmd
php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
The defaults configuration settings are set in config/dompdf.php. Copy this file to your own config directory to modify the values. You can publish the config using this command:
Step : 3 Create route
After done configuration then create one route in your routes/web.php file
Route::get('generate-pdf', 'PdfGenerateController@pdfview')->name('generate-pdf');
Step : 4 Create controller
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use PDF;
class PdfGenerateController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function pdfview(Request $request)
{
$users = DB::table("users")->get();
view()->share('users',$users);
if($request->has('download')){
// Set extra option
PDF::setOptions(['dpi' => 150, 'defaultFont' => 'sans-serif']);
// pass view file
$pdf = PDF::loadView('pdfview');
// download pdf
return $pdf->download('pdfview.pdf');
}
return view('pdfview');
}
}
Step : 5 Create view
[ADDCODE]
Now we are create one simple blade file in resources/views/pdfview.blade.php file and here we are make very simple html layout for generate pdf file.
<!DOCTYPE html>
<html>
<head>
<title>User list - PDF</title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<a href="{{ route('generate-pdf',['download'=>'pdf']) }}">Download PDF</a>
<table class="table table-bordered">
<thead>
<th>Name</th>
<th>Email</th>
</thead>
<tbody>
@foreach ($users as $key => $value)
<tr>
<td>{{ $value->name }}</td>
<td>{{ $value->email }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</body>
</html>
Note : if you show following error when generate pdf
ErrorException in AdobeFontMetrics.php line 45:
fopen(/var/www/Laravel/LaraDemo/storage/fonts//c47afe5539ba1b2094563d54dce2def7.ufm): failed to open stream: No such file or directory
Solution : please create one empty fonts folder in storage derectory and give it permision 777.
Now we are ready to run our example so run bellow command ro quick run:
php artisan serve
Now you can open bellow URL on your browser:
http://localhost:8000/generate-pdf
I hope it can help you...
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com
How to send webhook in Laravel 8
In this article, i will share with you h...Angular9 - Routing And Navigation Example
1. Introduction to Angular 7|8|9 Routing...How to toggle text inside and element on click using jQuery
Use the jQuery text() method You can...How to determine if variable is undefined or null in JavaScript
Use the equality operator (==) In Jav...How to Include a JavaScript File in another JavaScript File
Use the export and import Statement S...