Table of contents

laravel fpdf

In this post, I will show you an example of how to implement Laravel 9 using FPDF. Let's use FPDF as our package to generate PDF with Laravel.

 

In my previous post, I have several examples of PDFs with other packages. Now let's do it with FPDF.

 

Step 1: Laravel Installation

If you don't have a Laravel 9 install in your local just run the following command below:

composer create-project --prefer-dist laravel/laravel laravel-fpdf

cd laravel-fpdf

 

Step 2: Install FPDF Package

To generate PDF in Laravel we need to install laravel-fpdf package. Run the following command below:

composer require codedge/laravel-fpdf

 

Step 3: Setup Routes and Controller

Let's create routes and controllers for our Laravel FPDF generator.

 

routes.php/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PdfController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('pdf', [PdfController::class, 'index']);

 

Run the command below to make a controller:

php artisan make:controller PdfController

 

Step 4: Laravel FPDF Example

Then edit the PdfController generated. See below:

<?php

namespace App\Http\Controllers;

use Codedge\Fpdf\Fpdf\Fpdf;
use Illuminate\Http\Request;

class PdfController extends Controller
{
	protected $fpdf;
 
    public function __construct()
    {
        $this->fpdf = new Fpdf;
    }

    public function index() 
    {
    	$this->fpdf->SetFont('Arial', 'B', 15);
        $this->fpdf->AddPage("L", ['100', '100']);
        $this->fpdf->Text(10, 10, "Hello World!");       
         
        $this->fpdf->Output();

        exit;
    }
}

 

Here is the result:

laravel fpdf

 

Now you have a basic how to work with the Laravel FPDF package. I hope it helps.

 

To learn more about this package please visit here.

 

Thank you for visiting :)