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.

In this article, we will step by step create PDF file with digital signature certificate in TCPDF library.

Step 1: Create Laravel project

First create Laravel project from the Terminal using bellow command.

composer create-project laravel/laravel tcpdf

Step 2: Install TCPDF library

We wil use elibyy/tcpdf-laravel package which makes simple and easy to use TCPDF in Laravel. You can also direct Install TCPDF package and use.

First go to the project root directory in Terminal and run the command.

composer require elibyy/tcpdf-laravel

It will install library in the vendor folder.

Step 3: Configure package

Open config/app.php file and add provider class in the providers array.

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

Also add facade class in the aliases array.

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

Step 4: Create new route

Now create route in routes/web.php file. Add this bellow line to create new URL.

<?php

Route::get('/tcpdf', 'HomeController@createPDF')->name('createPDF');

Step 5: Create new controller class and method

Now create a new controller class with the bellow command.

php artisan make:controller HomeController

And add createPDF method in the HomeController.php. Don't forget to use PDF class in the top of the class declaration.

<?php

namespace App\Http\Controllers;

use PDF;
use Illuminate\Http\Request;

class HomeController 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' => 'TCPDF',
            'Location' => 'Office',
            'Reason' => 'Testing TCPDF',
            'ContactInfo' => 'http://www.tcpdf.org',
        );

        // set document signature
        PDF::setSignature($certificate, $certificate, 'tcpdfdemo', '', 2, $info);
        
        PDF::SetFont('helvetica', '', 12);
        PDF::SetTitle('Hello World');
        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');
    }
}

See the available class methods to customize your PDF file. Output() method will save PDF file in the passed location.

Also check certificate location variable is prefix with 'file://', otherwise it will return bellow error.

openssl_pkcs7_sign(): error getting cert

Step 6: Create blade view

We are also adding content of the blade file. I have added blade file in the view() method. You can also direct add html view content or via view() method. Here is my resource/views/tcpdf.blade.php file sample file.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>TCPDF</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 Arobat 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>

Step 7: Generate PDF file

That's it. Now run your Laravel project with php artisan serve command and open your route URL in the browser. A new PDF file is generated in the public folder.

To check if the certificate is installed perfectly, open PDF file in the Adobe reader in Windows operating system and click on the image bellow in the PDF document. It will load certificate.

Conclusion

There are so many features and customizations available in TCPDF. You can check  I hope you will like this article. Also please follow us on Twitter and Like our Facebook page for updates.


Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

OR

4 Comments

  • Image
    umar
    February 26, 2020 Reply

    whrere is crt file i am confusing about crt file plese explain

  • Image
    ramity
    February 06, 2020 Reply

    thanks for help

    after
    echo(file_get_contents($certificate));
    get this error
    failed to open stream: No such file or directory.

  • Image
    ramity
    February 06, 2020 Reply

    hi,
    i use this to my project, but getting this error
    openssl_pkcs7_sign(): error getting private key
    can you help me?

    Image
    Jitesh Meniya
    Thu February, 2020

    Either you certificate file is not found. To check this, print $certificate variable.

    $certificate = 'file://'.base_path().'/public/tcpdf.crt';
    echo(file_get_contents($certificate));

    If you see certificate content, then there must be your certificate is not valid.

    Or you may be this will help you:

    https://stackoverflow.com/questions/32805518/warning-openssl-pkcs7-sign-error-getting-private-key-on-linux-server

  • Image
    amit
    January 13, 2020 Reply

    super cool post! Your post is very needful for me ,thanks for sharing this.