How to Add Text on Existing PDF File in Laravel?

In many Laravel applications, especially those involving forms, certificates, reports, or contracts, you may want to Add Text into specific positions on an existing PDF file. This guide will walk you through how to do just that using the FPDF library.

Let’s walk through the process step by step.

Prerequisites

Before we start, make sure you have:

  • Laravel 8/9/10/11 installed
  • Composer configured
  • An existing PDF file (e.g., sample.pdf)
  • PHP version 7.4 or higher
  • setasign/fpdi (extension of FPDF that allows importing existing PDFs)

Step 1: Install FPDF and FPDI

Run the following composer command to install FPDF + FPDI:

composer require setasign/fpdf
composer require setasign/fpdi

Step 2: Create a Controller

php artisan make:controller PdfEditController

Step 3: Place Your PDF Template

Place sample.pdf in storage/app/public/pdf_templates/.

Step 4: Write Logic to Add Text on PDF

use setasign\Fpdi\Fpdi;
use Illuminate\Support\Facades\File;

public function addTextToPdf()
{
    $pdf = new Fpdi();

    $filePath = storage_path('app/public/pdf_templates/sample.pdf');
    $pageCount = $pdf->setSourceFile($filePath);

    for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
        $templateId = $pdf->importPage($pageNo);
        $size = $pdf->getTemplateSize($templateId);

        $pdf->AddPage($size['orientation'], [$size['width'], $size['height']]);
        $pdf->useTemplate($templateId);

        // Set font and position for text
        $pdf->SetFont('Arial', '', 12);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->SetXY(35, 99); // X, Y position in mm
        $pdf->Write(0, '1/52c, Drive In Rd, Goyal Intercity');
        $pdf->SetXY(35, 104);
        $pdf->Write(0, 'Nilmani Society, Thaltej, Ahmedabad');

        // Optional: Add signature image
        $signaturePath = public_path('images/signature.png');
        if (file_exists($signaturePath)) {
            $pdf->Image($signaturePath, 150, 250, 40, 20);
        }
    }

    // Output file
    $outputPath = storage_path('app/public/generated_pdfs/output.pdf');

    if (!File::isDirectory(dirname($outputPath))) {
        File::makeDirectory(dirname($outputPath), 0755, true);
    }

    $pdf->Output('F', $outputPath);

    return response()->file($outputPath);
}

Step 5: Add Route

In routes/web.php

Route::get('/edit-pdf', [PdfEditController::class, 'addTextToPdf']);

Step 6: 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:

http://localhost:8000/edit-pdf

Your browser will display the updated PDF with the text added to the original file.

Tips & Use Cases

  • Automate Certificates: Add names, scores, or QR codes to pre-designed templates.
  • Invoices/Receipts: Overlay values on blank invoice layouts.
  • Forms: Add user data dynamically on government or application forms.
  • Contracts: Add digital signatures or authorized details on scanned agreements.

Conclusion

Using FPDF + FPDI in Laravel, you can effortlessly add text at specific positions on existing PDF files, whether you’re generating documents, personalizing templates, or inserting images like signatures.

With just a few lines of code, Laravel becomes a powerful PDF editing tool—without using external services.

Scroll to Top