Code And Deploy
Posted on May 16, 2022
Originally posted @ https://codeanddeploy.com visit and download the sample code:
https://codeanddeploy.com/blog/laravel/how-to-implement-laravel-8-using-fpdf-example
In this post, I will show you an example how to implement Laravel 8 using FPDF. Let's use FPDF as our package to generate PDF with Laravel.
In my previous post I have sereval examples about PDF with other packages. Now let's do with FPDF.
Step 1: Laravel Installation
If you don't have a Laravel 8 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 controller 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 Laravel FPDF package. I hope it helps.
To learn more about this package please visit here.
I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/jquery/how-to-check-if-image-is-loaded-or-not-in-jquery if you want to download this code.
Happy coding :)
Posted on May 16, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.