Laravel 6 - How to generate HTML to PDF with laravel domPDF
In this article we will share with you how to generate HTML to PDF in laravel help of barryvdh/laravel-dompdf. in laravel application many time you neet to generate report in PDF. so, barryvdh/laravel-dompdf package is best for generate your HTML content in PDF. and barryvdh/laravel-dompdf package is very easy to use in your laravel application.
if you never use barryvdh/laravel-dompdf before then don't worry in this tutorials we share with you step by step how to generate HTML to PDF in you laravel application.
In this tutorials we will generate static HTML file into domPDF but you can also generate dynamic HTML file with domPDF.
Step - 1 Install package
First we need to install barryvdh/laravel-dompdf
package in laravel application help of following commant.
composer require barryvdh/laravel-dompdf
Step - 2 Configure package
After install barryvdh/laravel-dompdf
package then you should configure some setting in laravel application. just following this small things. open you config/app.php
file and set following values into providers
array as well as 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 following line just before return $app
veriable
$app->singleton(\Barryvdh\DomPDF\ServiceProvider::class);
return $app;
Now, register DomPDF's app service provider then publish it's service provider run by following command in you terminal/cmd.
php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
After run this command the default configuration file automatic generated in config/dompdf.php
. you can modify this setting depend on requiremen.
Step - 3 Generate route
Now, create one GET route for generate HTML to PDF.
Route::get('generate-html-to-pdf', 'HtmlToPDFController@index')->name('generate-html-to-pdf');
Step - 4 Create controller file
After create route then we need to create also HtmlToPDFController
controller using following commant run in your termimnal or cmd.
php artisan make:controller HtmlToPDFController
After run this commant your HtmlToPDFController.php
file automatic created on app/Http/Controllers
folder. just open it and write following code into that file.
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use PDF;
class HtmlToPDFController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index(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 blade/view file
After done controller file then we create one laravel blade HTML file which generated into PDF file. 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.
<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>
<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>
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 the storage directory and give it permission 777.
We hope these small tutorials help everyone. if you like this article then please comment below. Thanks..
Copyright 2023 HackTheStuff