Laravel 10 Digital Signature PDF Tutorial

Laravel 10 Digital Signature PDF Tutorial

To add a digital signature on pdf in laravel 10; In this tutorial, you will learn how to add a digital signature on pdf using the DomPDF TCPDF library in Laravel 10 apps.

How to Add Digital Signature on PDF in Laravel 10

Let’s get started with adding a digital signature on PDF in laravel 10 apps:

  • Step 1: Create a New Laravel Project
  • Step 2: Install Dependencies
  • Step 3: Configure Laravel DomPDF
  • Step 4: Set Up TCPDF for Digital Signatures
  • Step 5: Create a Controller
  • Step 6: Create a Route
  • Step 7: Generate a PDF with Digital Signature

Step 1: Create a New Laravel Project

First of all, open your cmd or terminal and execute the following command into it to install and setup new laravel 10 app into server:

composer create-project --prefer-dist laravel/laravel digital-signature-pdf
cd digital-signature-pdf

Step 2: Install Dependencies

Next, execute the following commands to install laravel-dompdf for PDF generation and tcpdf for digital signatures:

composer require barryvdh/laravel-dompdf
composer require tecnickcom/tcpdf

Step 3: Configure Laravel DomPDF

Once you have installed laravel-dompdf into your laravel project. Now you need to configure dompdf with laravel apps.

So, Open the config/app.php file and add the following line to the providers array:

Barryvdh\DomPDF\ServiceProvider::class,

Next, add the following line to the aliases array in the same file:

'PDF' => Barryvdh\DomPDF\Facade::class,

Step 4: Set Up TCPDF for Digital Signatures

Next, You need to configure TCPDF for digital signatures. Create a new file named tcpdf_signature_config.php in the root directory of your Laravel project with the following content:

<?php
return [
    'signature_title' => 'Digital Signature',
    'signature_title_page' => 'Digital Signature - Page %s',
    'signature_font_size' => 8,
    'signature_image_width' => 25,
    'signature_image_height' => 25,
    'signature_file' => '', // Path to your signature image file
    'signature_coordinates' => [
        'x' => 10,
        'y' => 10,
    ],
];

Important Note:- Replace 'signature_file' with the actual path to your signature image file.

Step 5: Create a Controller

Next, open again your cmd and execute the following command into it to create a new controller for handling PDF generation and digital signatures:

php artisan make:controller PDFSignatureController

Once you have created controller class. Next, you need to edit the PDFSignatureController.php file in the app/Http/Controllers directory with the following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;

class PDFSignatureController extends Controller
{
    public function generatePDFWithSignature()
    {
        // Load TCPDF configuration
        $config = config('tcpdf_signature_config');

        // Create a new TCPDF instance
        $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

        // Set document properties
        $pdf->SetCreator(config('app.name'));
        $pdf->SetAuthor('Your Name');
        $pdf->SetTitle('Sample PDF with Digital Signature');
        $pdf->SetSubject('Digital Signature Example');
        $pdf->SetKeywords('laravel, pdf, digital signature');

        // Set header and footer fonts
        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

        // Set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        // Set margins
        $pdf->SetMargins(10, 10, 10);

        // Set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, 10);

        // Set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        // Initialize PDF document
        $pdf->AddPage();

        // Output digital signature image
        if (!empty($config['signature_file'])) {
            $signatureX = $config['signature_coordinates']['x'];
            $signatureY = $config['signature_coordinates']['y'];
            $pdf->Image($config['signature_file'], $signatureX, $signatureY, $config['signature_image_width'], $config['signature_image_height']);
        }

        // Output text for digital signature
        $pdf->SetFont('helvetica', '', $config['signature_font_size']);
        $pdf->SetXY(40, 20);
        $pdf->Cell(0, 0, 'Digitally signed by:', 0, 1);
        $pdf->SetXY(40, 25);
        $pdf->Cell(0, 0, 'Your Name', 0, 1);
        $pdf->SetXY(40, 30);
        $pdf->Cell(0, 0, 'Date: ' . date('Y-m-d H:i:s'), 0, 1);

        // Output PDF to the browser
        $pdf->Output('sample.pdf', 'I');
    }
}

Step 6: Create a Route

In your routes/web.php file, add a route to your newly created controller method:

use App\Http\Controllers\PDFSignatureController;

Route::get('/generate-pdf', [PDFSignatureController::class, 'generatePDFWithSignature']);

Step 7: Generate a PDF with Digital Signature

Now, you can generate a PDF with a digital signature by visiting the following URL in your browser:

http://your-domain/generate-pdf

Conclusion

Congratulations 🎊! You have successfully implemented digital signature functionality for PDF documents in Laravel 10 using TCPDF and Laravel DomPDF.

Recommended Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *