In this post, we are going to know about generating PDF documents using TCPDF Package in Laravel. Most of the application needs to generate pdf documents for many reasons. It may use to produce E-Document for end user knowledge or reference. It helps to prepare Invoice bills, profile information, reports download etc. In real-time many open sources are there to generating PDF in laravel. Here we going to learn and utilize TCPDF package to generate PDF in laravel.
Nowadays TCPDF is one of the world’s extremely powerful Free Source package, most of the web application and CMS are using this to generate PDF’s. TCPDF does not require any codebase and executable file to configure this package. Utilizing package is very very simple just we need to including the configure file in our pages in an application is enough.
Including Package
To utilize TCPDF in Laravel, we need to include “elibyy/tcpdf-laravel” package in our application. Following line, help to include TCPDF package in your composer.json file.
1 2 3 4 |
"require": { ..... "elibyy/tcpdf-laravel": "5.2.*" } |
Implementation package
Here we need to include library file in our application. Here provider and aliases are going to update based on Package. Following line, help to include provider and aliases name in your config/app.php file.
1 2 3 4 5 6 7 8 9 10 |
'providers' => [ ... Elibyy\TCPDF\ServiceProvider::class, ], 'aliases' => [ ... 'PDF' => Elibyy\TCPDF\Facades\TCPDF::class ] |
Create Controller and Setup and define methods
Create a controller with required namespace and methods to handle the PDF generation URL request. Following command help you to create a controller in laravel application. Add the methods for PDF generation, Save PDF, Download PDF and HTML to PDF.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use PDF; class PdfDemoController extends Controller { public function index(){ return view('PdfDemo'); } public function samplePDF() { $html_content = '<h1>Generate a PDF using TCPDF in laravel </h1> <h4>by<br/>Learn Infinity</h4>'; PDF::SetTitle('Sample PDF'); PDF::AddPage(); PDF::writeHTML($html_content, true, false, true, false, ''); PDF::Output('SamplePDF.pdf'); } public function savePDF() { $html_content = '<h1>Generate a PDF using TCPDF in laravel </h1> <h4>by<br/>Learn Infinity</h4>'; PDF::SetTitle('Sample PDF'); PDF::AddPage(); PDF::writeHTML($html_content, true, false, true, false, ''); PDF::Output(public_path(uniqid().'_SamplePDF.pdf'), 'F'); } public function downloadPDF() { $html_content = '<h1>Generate a PDF using TCPDF in laravel </h1> <h4>by<br/>Learn Infinity</h4>'; PDF::SetTitle('Sample PDF'); PDF::AddPage(); PDF::writeHTML($html_content, true, false, true, false, ''); PDF::Output(uniqid().'_SamplePDF.pdf', 'D'); } public function HtmlToPDF() { $view = \View::make('HtmlToPDF'); $html_content = $view->render(); PDF::SetTitle('Sample PDF'); PDF::AddPage(); PDF::writeHTML($html_content, true, false, true, false, ''); PDF::Output(uniqid().'_SamplePDF.pdf'); } } |
Route Definition
Here we need to define URL request with respected Controller methods.
1 2 3 4 5 |
Route::get('/PdfDemo', ['as'=>'PdfDemo','uses'=>'PdfDemoController@index']); Route::get('/sample-pdf', ['as'=>'SamplePDF','uses'=>'PdfDemoController@samplePDF']); Route::get('/save-pdf', ['as'=>'SavePDF','uses'=>'PdfDemoController@savePDF']); Route::get('/download-pdf', ['as'=>'DownloadPDF','uses'=>'PdfDemoController@downloadPDF']); Route::get('/html-to-pdf', ['as'=>'HtmlToPDF','uses'=>'PdfDemoController@htmlToPDF']); |
Define an HTML Blade page
In this section, we design an HTML Blade page with Proper URL request link buttons. HTML page for PDF Generation with the required information for conversion into PDF File.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
@extends('layouts.app') @section('content') <style type="text/css"> h3.center-text { text-align: center; } </style> <div class="container"> <h3 class="center-text">Generate a PDF using TCPDF in laravel - Learn Infinity</h3> <div class="row"> <div class="col-md-offset-2 col-md-8"> <br/> <br/> <br/> <br/> <table cellspacing="3" cellpadding="5" width="100%"> <tr> <td width="25%"> <div class="form-group"> <a href="{{ route('SamplePDF') }}" class="btn btn-primary">Generate Sample PDF</a> </div> </td> <td width="25%"> <div class="form-group"> <a href="{{ route('SavePDF') }}" class="btn btn-primary">Save Sample PDF</a> </div> </td> <td width="25%"> <div class="form-group"> <a href="{{ route('DownloadPDF') }}" class="btn btn-primary">Download Sample PDF</a> </div> </td> <td width="25%"> <div class="form-group"> <a href="{{ route('HtmlToPDF') }}" class="btn btn-primary">Html To PDF</a> </div> </td> </tr> </table> </div> </div> </div> @endsection |
and HTML to PDF file blade page with following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<h1>Generate a PDF using TCPDF in laravel </h1> <h4>by<br/>Learn Infinity</h4> <table border="1" width="100%" cellpadding="10" > <tr> <th width="10%">SNo.</th> <th width="40%">Name</th> <th width="50%">Email</th> </tr> @for($i=1; $i<=15; $i++) <tr> <td align="center">{{$i}}</td> <td>User {{$i}}</td> <td>useremail{{$i}}@li.com</td> </tr> @endfor </table> |
Now you can run the proper URL address to see the demo for PDF generation in your application.