A digital signature is a mathematical scheme for verifying the authenticity and integrity of a digital message or document. A valid digital signature on a message gives a recipient confidence that the message was sent by a known sender, and that the message has not been altered since it was signed.
In this tutorial, you will learn how to add a digital signature on pdf using the DomPDF TCPDF library in Laravel 10 apps. To add a digital signature on PDF in Laravel 10, follow these steps.
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
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
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,
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.
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');
}
}
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']);
Now, you can generate a PDF with a digital signature by visiting the following URL in your browser:
http://your-domain/generate-pdf
You have successfully implemented digital signature functionality for PDF documents in Laravel 10 using TCPDF and Laravel DomPDF.