How to Write Text on Existing PDF File in 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 itsolutionstuff.com 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.
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.
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.
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 = "itsolutionstuff.com";
$fpdi->Text($left,$top,$text);
$fpdi->Image("https://www.itsolutionstuff.com/assets/images/footer-logo.png", 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:
http://localhost:8000/fill-data-pdf
Output:
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Convert JSON to Array in Laravel?
- Laravel Cashier Stripe Subscription Example Tutorial
- How to Use Google Translator in Laravel?
- Laravel 9 Send Email with PDF Attachment Example
- How to Merge Multiple PDF Files in Laravel 9?
- How to Add Password Protection for PDF File in Laravel?
- Laravel Convert PDF to Image Example
- Laravel CRUD with Image Upload Tutorial
- How to use Laravel Model Observers?
- Laravel Fullcalendar Example Tutorial
- How to Create Multilingual Website in Laravel?
- Laravel Create PDF File with Image Example
- How to Generate PDF with Graph in Laravel?