How to Write Text on Existing PDF in Laravel

Hello Artisan,

In this tutorial, I will show you dynamically adding text and images to existing pdf using php laravel application. To create this laravel write to existing pdf example, I will use two packages like setasign/fpdfand other is setasign/fpdi to complete this example.

You know that sometimes we need to edit our existing pdf file in the laravel application. In this example, I will take one pdf file and add text as codecheef.org with the image on that pdf file. Then we will save that file as a pdf file also. so let's see the below step to do this example.

add-text-existing-pdf-laravel

 

Step 1 : Install Laravel

In this step, we need to get a fresh Laravel version application using the bellow command, So open your terminal OR command prompt and run the below command:

composer create-project laravel/laravel example-app

 

Step 2: Install fpdf and fpdi Package

Now, we need to install fpdf and fpdi packages to edit pdf file. so, let's run the below commands:

composer require setasign/fpdf
composer require setasign/fpdi

 

Step 3: Create Route

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

routes/web.php

use App\Http\Controllers\TestController;
use Illuminate\Support\Facades\Route;

Route::get('pdf', [TestController::class,'index']);

 

Step 4: Create Controller

In this step, You need to create TestController with index and processTextOnPDFFile method. Then you need to download sample.pdf file from here: Download Sample.pdf File. Then put that file on a public folder or laravel app.

app/Http/Controllers/TestController.php

namespace App\Http\Controllers;

use setasign\Fpdi\Fpdi;
use Illuminate\Http\Request;

class TestController extends Controller
{
    public function index(Request $request)
    {
        $filePath = public_path($your_pdf_file_path);
        $outputFilePath = public_path($your_output_pdf_file_path);
        $this->processTextOnPDFFile($filePath, $outputFilePath);
          
        return response()->file($outputFilePath);
    }

    public function processTextOnPDFFile($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(153,0,153);
  
            $left = 10;
            $top = 10;
            $text = "your_dynamic_text_goes_here";
            $fpdi->Text($left,$top,$text);
  
            $fpdi->Image("https://www.toonpool.com/cartoons/Google_4388", 40, 90);
        }
  
        return $fpdi->Output($outputFilePath, 'F');
    }
}

 

Read also: Laravel 9 Model Events Every Developer Should Know

 

Hope it can help you.

 

author-image
Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js