How to Write Text on Existing PDF File in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

Hello Friends,

This tutorial will provide an example of how to write text on existing pdf in laravel. you can see how to edit pdf file in laravel. We will look at an example of fpdf edit existing pdf laravel. Here you will learn laravel add image on existing pdf file.

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 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 image using setasign/fpdf and setasign/fpdi composer package.

In this example, i will take one sample.pdf file and add text as with image on that pdf file. Then we will save that file as sample_output.pdf file. so let's see the below step to do this example.

Step 1 : Install Laravel

first of all we need to get fresh Laravel version application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project laravel/laravel example-app

Step 2: Install fpdf and fpdi Package

here, we will install fpdf and fpdi package for edit pdf file. so, let's run the bellow commands:

composer require setasign/fpdf

composer require setasign/fpdi

Step 3: Create Route

In this is step we need to create one route for edit pdf file. let's add the below route on web.php file.



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.

Then you need to download sample.pdf file from here: Download Sample.pdf File.

Then put that file on public folder or laravel app.

Add the below code on controller file.



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->SetFont("helvetica", "", 15);


$left = 10;

$top = 10;

$text = "";


$fpdi->Image("", 40, 90);


return $fpdi->Output($outputFilePath, 'F');



Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:



I hope it can help you...

Tags :