switchon@lavalamp.biz
+27(0) 83 419 4851 / +27(0) 21 036 1165
Application & software development
Get A Quote

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact
    the importance of website maintenance
    The Importance Of Website Maintenance
    Jul 2, 2019
    sage api
    Getting Your Sage One Company ID For API Integration
    Sep 6, 2019

    How To Create And Digitally Sign A PDF Using Laravel And TCPDF

    Categories
    • Web Development
    Tags
    • laravel
    • pdf
    • PHP
    • tcpdf
    • web development
    how to create and digitally sign a pdf using laravel and tcpdf
    how to create and digitally sign a pdf using laravel and tcpdf

     

    There are many reasons to digitally sign a PDF. The main 2 being to prove that a PDF hasn’t been edited and to prove that you are the author of the said PDF. You’re going to need 2 things for this tutorial. A certificate (e.g. alice.crt) and a private key (e.g. alice.key). I have provided some example certificates.

     

    Download Example Certificates.

     

    To generate PDFs, we can use TCPDF. TCPDF is a free, open source PHP class for generating PDFs. You can manually import TCPDF into any PHP project, but to make our lives easier there is a free Laravel service provider that will allow us to easily use TCPDF in any Laravel project. You can read more about the Laravel service provider here.

     

     

    Generating the PDF

     

    First lets begin by generating installing tcpdf-laravel.

    composer require elibyy/tcpdf-laravel

    If you don’t use auto-discovery, then add the following to your ‘config/app.php’.

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

    Now import tcpdf-laravel into your project wherever you wish to use it:

    use Elibyy\TCPDF\Facades\TCPDF;

    You can now use tcpdf to generate PDFs. We will do this using Laravels views. What we are doing is creating HTML code and then using TCPDF to render that into a PDF.

    $view = view('pdf', [
    'content' = 'Lorem ipsum dolor'
    ]);
    $html = $view->render();
    
    $pdf = new TCPDF();
    $pdf::AddPage();
    $pdf::writeHTML($html);
    
    $pdf_path = env('STORAGE_PATH') . 'https://lavalamplab.b-cdn.net/pdf_name.pdf';
    $pdf_out = $pdf::Output($pdf_path, 'F');
    

    The above code renders the pdf.blade.html file into $html and then uses tcpdf to generate it into a pdf and store it somewhere on disk. You can just output the PDF to the client by replacing $pdf_out = $pdf::Output($pdf_path, 'F'); with $pdf::Output();

    Now we can do the same thing and also digitally sign the PDF in the process.

    $certificate = 'file://'. realpath('../storage/cert/alice.crt');
    $private_key = 'file://'. realpath('../storage/cert/alice.key');
    
    $view = view('pdf', [
    'content' = 'Lorem ipsum dolor'
    ]);
    $html = $view->render();
    
    $pdf = new TCPDF();
    $pdf::AddPage();
    $pdf::writeHTML($html);
    
    // set additional information
    $info = array(
    'Name' => 'Name of PDF',
    'Location' => '',
    'Reason' => 'Proof of author',
    'ContactInfo' => 'info@example.co.za',
    );
    
    // set document signature
    $pdf::setSignature($certificate, $private_key, '', '', 2, $info);
    
    $pdf_path = env('STORAGE_PATH').'https://lavalamplab.b-cdn.net/files/pdf_name.pdf';
    $pdf_out = $pdf::Output($pdf_path, 'F');
    

    You need to use the realpath() function to get the path to the key and certificate.

     

    If you require more information about TCPDF I highly recommend these 2 resources:

     

    1. TCPDF digital signature examples page
    2. The TCPDF class documentation.

    Contact us


      Related posts:

      laravel livewireLaravel Livewire laravel envoyer and forgeProvisioning A New Digital Ocean Server With Laravel Forge, And Deploying Code With Laravel Envoyer implementing laravel echoImplementing Laravel Echo And Socket.io With A Laravel API And Ionic Vue App how to deploy your app/website on herokuHow To Deploy Your App/Website On Heroku
      Share
      23
      Hristo Mitev
      Hristo Mitev
      Hristo is a frontend developer with knowledge and experience in Laravel.

      3 Comments

      1. Walfrido says:
        Nov 6, 2019 at 9:17 pm

        Hi, it’s works for me. thank so much!

        Reply
      2. Danzel says:
        Nov 14, 2020 at 12:27 am

        You should avoid using env() helper outside config! It will break your app when you cache the config.

        Reply
      3. marcelo pereira says:
        Nov 28, 2020 at 12:10 am

        I need to sign the same document with two different certificates. Can you help me?

        Reply

      Leave a Reply Cancel reply

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

      Lava Lamp Lab


      Like technology, a lava lamp constantly changes form, producing new conditions with every passing moment



      lava lamp lab facebook   lava lamp lab twitter   lava lamp lab linkedin   lava lamp lab instgram

      Services


      Application & software development

      Outsourced software development

      Project based resourcing

      Digital marketing & consulting

      Graphic design & consulting

      UI / UX design & consulting

      Contact Us


      +27(0) 83 419 4851

      +27(0) 21 036 1165


      switchon@lavalamp.biz


      Unit 4 Monaco Square,
      14 Church Street,
      Durbanville,
      Cape Town, 7550

      NBConsult Group


      nbconsult
      nbconnect msp
      nbclearning
      river broadband
      designer needed
      © 2023 Lava Lamp Lab (Pty) Ltd | All Rights Reserved | Privacy Policy
      Contact us now

        Application & software development

          Outsourced software development

            Project based resourcing

              Digital marketing & consulting

                Graphic design & consulting

                  UI/UX design & consulting

                    Lease an expert

                      Recruitment services

                        We are using cookies to give you the best experience on our website.

                        You can find out more about which cookies we are using or switch them off in settings.

                        Lava Lamp Lab
                        Powered by  GDPR Cookie Compliance
                        Privacy Overview

                        This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

                        Strictly Necessary Cookies

                        Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

                        If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.

                        3rd Party Cookies

                        This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

                        Keeping this cookie enabled helps us to improve our website.

                        Please enable Strictly Necessary Cookies first so that we can save your preferences!

                        Cookie Policy

                        More information about our Cookie Policy