How to Edit a PDF File in Laravel?

03-Nov-2022

.

Admin

How to Edit a PDF File in Laravel?

Hello Friends,

Suppose you need to see an example of how to edit a pdf file in laravel. We will look at an example of fpdf editing existing pdf laravel. This article will give you a simple example of laravel adding an image to an existing pdf file. I would like to show you a 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 setasign/fpdf and setasign/fpdi composer packages.

In this example, I will take one sample.pdf file and add text as nicesnippets.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 Laravel


first 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-app

Step 2: Install fpdf and fpdi Package

here, we will install the fpdf and fpdi packages to edit the pdf file. so, let's run the bellow 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 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(153,0,153);

$left = 10;

$top = 10;

$text = "NiceSnippets.com";

$fpdi->Text($left,$top,$text);

$fpdi->Image("file:///var/www/example-app/public/nice-logo.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-pdf

Output:

I hope it can help you...

#Laravel