How To Add Digital Signature In PDF In Laravel 9

Websolutionstuff | Dec-21-2022 | Categories : Laravel PHP

In this article, we will see how to add a digital signature in pdf in laravel 9. Here, we will learn to add a digital signature certificate in pdf in laravel 8 and laravel 9. We will use elibyy/tcpdf-laravel package.

TCPDF is an open-source PHP library and the only PHP-based library that includes complete support for UTF-8 Unicode and right-to-left languages, including the bidirectional algorithm. Also, TCPDF is not supported in PHP 7.

So, let's see laravel 9 add a digital signature in pdf, add a digital signature to the pdf file, and add a digital signature certificate in pdf in laravel 7/8/9.

Step 1: Install Laravel 9

Step 2: Install TCPDF Package

Step 3: Configure TCPDF Package

Step 4: Add Routes

Step 5: Create Controller

Step 6: Create Blade File

 

Step 1: Install Laravel 9

In this step, we will install laravel 9 using the following command.

composer create-project --prefer-dist laravel/laravel laravel9_digital_signature

 

Step 2: Install TCPDF Package

The Laravel TCPDF service provider can be installed via composer by requiring the elibyy/tcpdf-laravel package in your project's composer.json. So, run the following command in the terminal.

composer require elibyy/tcpdf-laravel

 

 

Step 3: Configure TCPDF Package

If you are using a lower version of laravel like 5.5+ then need to add providers and aliases to the config.php file.

'providers' => [
    //...
    Elibyy\TCPDF\ServiceProvider::class,
]

//...

'aliases' => [
    //...
    'PDF' => Elibyy\TCPDF\Facades\TCPDF::class
]

 

Step 4: Add Routes

Now, we will add routes to the web.php file.

routes/web.php

<?php

use App\Http\Controllers\PDFController;

Route::get('/create/pdf', [PDFController::class, 'createPDF'])->name('createPDF');

 

Step 5: Create Controller

In this step, we will create a PDFController file using the following command.

php artisan make:controller PDFController

app/Http/Controllers/PDFController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;

class PDFController extends Controller
{
    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    public function createPDF(Request $request)
    {
        // set certificate file
        $certificate = 'file://'.base_path().'/public/tcpdf.crt';

        // set additional information in the signature
        $info = array(
            'Name' => 'Websolutionstuff',
            'Location' => 'Office',
            'Reason' => 'Websolutionstuff',
            'ContactInfo' => 'http://www.websolutionstuff.com',
        );

        // set document signature
        PDF::setSignature($certificate, $certificate, 'tcpdfdemo', '', 2, $info);
        
        PDF::SetFont('helvetica', '', 12);
        PDF::SetTitle('Websolutionstuff');
        PDF::AddPage();

        // print a line of text
        $text = view('tcpdf');

        // add view content
        PDF::writeHTML($text, true, 0, true, 0);

        // add image for signature
        PDF::Image('tcpdf.png', 180, 60, 15, 15, 'PNG');
        
        // define active area for signature appearance
        PDF::setSignatureAppearance(180, 60, 15, 15);
        
        // save pdf file
        PDF::Output(public_path('hello_world.pdf'), 'F');

        PDF::reset();

        dd('pdf created');
    }
}

Note: check certificate location variable is prefixed 'file://'. otherwise, it will return the below error.

openssl_pkcs7_sign(): error getting cert

 

 

Step 6: Create Blade File

In this step, we will create a tcpdf.blade.php file. So, add the following code to that file.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>How To Add Digital Signature In PDF In Laravel 9</title>
</head>
<body>
    <p>
        This is a <b color="#FF0000">digitally signed document</b> using the default (example) <b>tcpdf.crt</b> certificate.<br />To validate this signature you have to load the <b color="#006600">tcpdf.fdf</b> on the Acrobat Reader to add the certificate to <i>List of Trusted Identities</i>.<br /><br />For more information check the source code of this example and the source code documentation for the <i>setSignature()</i> method.<br /><br />
    </p>    
    <a href="http://www.tcpdf.org">www.tcpdf.org</a>
</body>
</html>

Now, we will run the laravel 9 add a digital signature certificate in the PDF application using the following command.

php artisan serve

 


You might also like:

Recommended Post
Featured Post
How To Backup Database In Laravel 9 Using Spatie
How To Backup Database In Lara...

In this article, we will see how to back up the database in laravel 9 using spatie. Here, we will learn automatic&n...

Read More

Feb-08-2023

jQuery Datatable Hide/Show Column Based On Condition
jQuery Datatable Hide/Show Col...

In this article, we will see a jquery datatable hide/show column based on condition. Here, we will learn how to hide and...

Read More

Jan-26-2023

Laravel 8 Datatables Keeping Selected Page Number
Laravel 8 Datatables Keeping S...

In this tutorial we will see laravel 8 datatables keeping selected page number after callback. In datatable page nu...

Read More

Dec-03-2021

How to Generate QR Code in Node.js
How to Generate QR Code in Nod...

In this example we will see how to generate QR Code in Node.js application. In this example we will use qrcode npm...

Read More

Sep-20-2021