Laravel Add Text to PDF File Example
Sep 30, 2022 . Admin

Hello Friends,
In this example, I will show you laravel add text to a pdf file example. if you want to see an example of how to edit a pdf file in laravel then you are in the right place. I would like to show you the laravel add image on an existing pdf file. I’m going to show you about laravel fpdf fpdi example. So, let's follow a few steps to create an instance of laravel setasign/fpdf.
You can use this example with laravel 6, laravel 7, laravel 8, and laravel 9 versions.
Sometimes, we need to edit an existing pdf file in the laravel application. You can not edit existing pdf with dompdf. However, we can add text and images using the setasign/fpdf and setasign/fpdi composer packages.
In this example, I will take one sample.pdf file and add text as mywebtuts.com with an image on that pdf file. Then we will save that file as a sample_output.pdf file. so let's see the below step to do this example.
Step 1: Install Laravelfirst of all we need to get a fresh Laravel version application using the bellow command, So open your terminal OR command prompt and run the bellow command:
composer create-project laravel/laravel example-appStep 2: Install fpdf and fpdi Package
here, we will install the fpdf and fpdi packages for editing the pdf file. so, let's run the bellow commands:
composer require setasign/fpdf composer require setasign/fpdiStep 3: Create Route
In this step we need to create one route for editing the pdf file. let's add the below route on the web.php file.
routes/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('fill-data-pdf', [PDFController::class,'index']);Step 4: Create Controller
in this step, we need to create PDFController with index() and fillPDFFile() method.
php artisan make:controller PDFController
Then put your sample.pdf file in the public folder.
Add the below code on the controller file.
app/Http/Controllers/PDFController.php<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use setasign\Fpdi\Fpdi; class PDFController extends Controller { /** * Write code on Method * * @return response() */ public function index(Request $request) { $filePath = public_path("sample.pdf"); $outputFilePath = public_path("sample_output.pdf"); $this->fillPDFFile($filePath, $outputFilePath); return response()->file($outputFilePath); } /** * Write code on Method * * @return response() */ public function fillPDFFile($file, $outputFilePath) { $fpdi = new FPDI; $count = $fpdi->setSourceFile($file); for ($i=1; $i<=$count; $i++) { $template = $fpdi->importPage($i); $size = $fpdi->getTemplateSize($template); $fpdi->AddPage($size['orientation'], array($size['width'], $size['height'])); $fpdi->useTemplate($template); $fpdi->SetFont("helvetica", "", 15); $fpdi->SetTextColor(1, 106, 254); $left = 10; $top = 10; $text = "Mywebtuts.com"; $fpdi->Text($left,$top,$text); $fpdi->Image("file:///var/www/example-app/public/logomy-web-tuts-new.png", 40, 90); } return $fpdi->Output($outputFilePath, 'F'); } }Step 5: Start Development Server
Start the development server. Use the PHP artisan serve command and start your server:
php artisan serve
Now you are ready to run our example so run the below command to quick run.
http://localhost:8000/fill-data-pdfOutput:

I hope it can help you...