Table of contents
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:
Â
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 :)
Read next