How to generate PDF in Laravel using anouar/fpdf

This tutorial is guide step by step procedure to generate PDF using anouar/fpdf  You can find the this package on packagist https://packagist.org/packages/anouar/fpdf

Steps:

  • Install package using composer in your project directory
  • Add service provider in app/config/app.php
  • Finally, add aliases in app/config/app.php
  • Now, Add your route
  • Serve the application

Now follow these steps:

For installation of package you can use this command:

composer require anouar/fpdf

 

Add the service provider in place:

'providers' => array(
	// ...

	AnouarFpdfFpdfServiceProvider::class,
)

 

Add the aliases in you app configuration:

'aliases' => array(
	// ...

	'Fpdf'	  => AnouarFpdfFacadesFpdf::class,
)

 

Run the command if you have any error regarding class not found etc…

composer dump-autoload

Now place the example code your route named pdf in  web.php:

Route::get('pdf', function(){
	$fpdf = new Fpdf();
        $fpdf->AddPage();
        $fpdf->SetFont('Arial','B',16);
        $fpdf->Cell(40,10,'Hello World!');
        $fpdf->Output();
        exit;

});

 

Now, serve your project:

php artisan serve

 

Check the output on localhost:80/pdf.

Hope its help you 🙂

Scroll to Top