Export PDF in Laravel Using Anouar FPDF

How to Export PDF in Laravel Using Anouar FPDFExport PDF in Laravel Using Anouar FPDF – To export data to PDF format in Laravel, requires a PDF library. FPDF is a PDF library is one of the most popular for PHP. FPDF library can not be directly inserted into Laravel, that’s why used Anouar FPDF library that supports installation using Composer, so it can be installed in Laravel.

Anouar FPDF Installation

First, install the library Anouar FPDF with this Composer command (via DOS Prompt) :
->cd \
->cd <htdocs>\<laravel>
->composer require anouar/fpdf

Configuration

Then open file <laravel>/config/app.php, add the following line :
providers' => [
/*
* Laravel Framework Service Providers...
*/
…….
Anouar\Fpdf\FpdfServiceProvider::class,

and

'aliases' => [
…
'Fpdf' => Anouar\Fpdf\Facades\Fpdf::class ,

Controller

To export the pdf, add this source code to the Controller :
use Fpdf;

…

public function exportpdf(){
 $pdf = new Fpdf();
 $pdf::AddPage();
 $pdf::SetFont('Arial','B',18);
 $pdf::Cell(0,10,"Title",0,"","C");
 $pdf::Ln();
 $pdf::Ln();
 $pdf::SetFont('Arial','B',12);
 $pdf::cell(25,8,"ID",1,"","C");
 $pdf::cell(45,8,"Name",1,"","L");
 $pdf::cell(35,8,"Address",1,"","L");
 $pdf::Ln();
 $pdf::SetFont("Arial","",10);
 $pdf::cell(25,8,"1",1,"","C");
 $pdf::cell(45,8,"John",1,"","L");
 $pdf::cell(35,8,"New York",1,"","L");
 $pdf::Ln();
 $pdf::Output();
 exit;
}

Routing

Do not forget to add routing, in file <laravel>/app/Http/routes.php add this line :
Route::get('exportpdf', 'YourController@exportpdf'); 

Result Export PDF in Laravel

The result can be run with the url: http: //<laravel>/public/exportpdf
How to Export PDF in Laravel Using Anouar FPDF

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>