Full Stack LAMP - MEAN Developer, Python developer. Certified Azure Developer. Freelance programmer/consultant/trainer.

How to write text on existing PDF using Laravel, PHP?

In this tutorial, you will learn how to write text to the existing pdf file in your Laravel application. We gonna write a simple PHP script to write text on a pdf file. This script will work with any version of the Laravel framework.

We gonna install the setasign\Fpdi library using composer and we’ll be using it to merge multiple pdf files into a single file.

Let’s start implementing it by installing the Laravel framework. This tutorial assumes that your computer already has PHP and Composer installed.

Step 1: Install the Laravel framework with the composer.
you can create a new Laravel project by using Composer directly with the below command. After the application has been created, you may start Laravel’s local development server using the Artisan CLI’s serve command as shown below.

composer create-project laravel/laravel example-app

cd example-app

php artisan serve

Step 2: Install FPDI Package using the composer

composer require setasign/fpdf
composer require setasign/fpdi

Step 3: Open the existing controller file or create a new controller with php artisan make:controller FillPDFController command and copy the below methods to it.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use setasign\Fpdi\Fpdi;

class FillPDFController extends Controller
    public function process(Request $request)
        // download sample file.
        Storage::disk('local')->put('test.pdf', file_get_contents('http://www.africau.edu/images/default/sample.pdf'));

        $outputFile = Storage::disk('local')->path('output.pdf');
        // fill data
        $this->fillPDF(Storage::disk('local')->path('test.pdf'), $outputFile);
        //output to browser
        return response()->file($outputFile);

    public function fillPDF($file, $outputFile)
        $fpdi = new FPDI;
        // merger operations
        $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']));
            $left = 10;
            $top = 10;
            $text = "arjunphp.com";
            $fpdi->SetFont("helvetica", "", 15);
        return $fpdi->Output($outputFile, 'F');

Step 4: Define a route in web.php to test the functions.

Route::get('/fill-data-pdfs', [\App\Http\Controllers\FillPDFController::class,'process']);

Step5: Now start your application with, php artisan serve and access it at http://localhost:8000/fill-data-pdfs

You can see the output files at:

  1. Thank you very much for this proof of concept demonstration. One correction, the class name in your route should be FillPDFController instead of MergePDFController.
    Have a great day!

Leave a Reply