How to Write Text on Existing PDF File in Laravel |
How to Write Text on Existing PDF File in Laravel |

How to Write Text on Existing PDF File in Laravel

Introduction: In this tutorial, we will explore the process of write text on an existing PDF file in Laravel framework. Manipulating PDF files is a common requirement in web applications, and Laravel provides convenient tools for accomplishing this task. By the end of this guide, you will have a clear understanding of how to integrate text into an existing PDF file seamlessly.

Prerequisites: Before we begin, make sure you have the following prerequisites installed:

  1. Laravel: Ensure that you have a Laravel application up and running. You can install Laravel using Composer by running the following command:
composer create-project --prefer-dist laravel/laravel your-project-name
  1. Composer: Make sure Composer is installed on your system. If not, you can download it from
  2. PDF Manipulation Library: Install a PDF manipulation library to assist in writing text on the PDF file. In this tutorial, we will use the popular library called “barryvdh/laravel-dompdf.” Install it using Composer:
composer require barryvdh/laravel-dompdf

Once these prerequisites are in place, we can proceed with the implementation.

Step 1: Configure Laravel-Dompdf:

Open your Laravel project and navigate to the config/app.php file. Add the following service provider and facade:

'providers' => [
    // ...

'aliases' => [
    // ...
    'PDF' => Barryvdh\DomPDF\Facade::class,

Run the following command to publish the configuration file:

php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"

Step 2: Create a Controller:

Generate a new controller by running the following command:

php artisan make:controller PdfController

Open the newly created PdfController.php file in the app/Http/Controllers directory.

Step 3: Write Code to Add Text to PDF:

Add the following code to your PdfController.php file to create a simple PDF with text:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;

class PdfController extends Controller
    public function writeTextOnPdf()
        $pdf = PDF::loadView('pdf.template', ['text' => 'Hello, Laravel PDF!']);

        return $pdf->stream('output.pdf');

This code defines a writeTextOnPdf method that generates a PDF using a Blade view named pdf.template and passes the text “Hello, Laravel PDF!” as a variable.

Step 4: Create Blade View:

Create a new Blade view file named template.blade.php in the resources/views/pdf directory with the following content:

<!DOCTYPE html>
    <title>Laravel PDF Tutorial</title>
    <h1>{{ $text }}</h1>

Step 5: Define a Route:

Open the web.php file in the routes directory and add a route to the writeTextOnPdf method:

use App\Http\Controllers\PdfController;

Route::get('/write-text-on-pdf', [PdfController::class, 'writeTextOnPdf']);

Step 6: Test the Implementation:

Run your Laravel development server using the following command:

php artisan serve

Visit http://localhost:8000/write-text-on-pdf in your browser to see the PDF with the added text.


Congratulations! You have successfully learned how to write text on an existing PDF file in Laravel using the barryvdh/laravel-dompdf library. Feel free to customize the code to suit your specific requirements and explore additional features offered by the library for more advanced PDF manipulations. Happy coding!

Leave a Reply