How to generate a PDF from html with TCPDF in laravel

How to generate a PDF from html with TCPDF in laravel

There are a couple of solutions to generate PDF's in laravel, DOMPDF , Snappy etc. In this case, we are going to teach you how to create a PDF using TCPDF.

TCPDF is now one of the world's most active Open Source projects, used daily by millions of users and included in thousands of CMS and Web applications.

TCPDF doesn't require anything else as its codebase. It doesn't use any executable and it's the first option for a lot of developers as it's easy to use an integrate.

Requirements

To use TCPDF in Laravel, we are going to include the tcpdf-laravel package in our project. You can add the package to your project adding the following line in your composer.json file :

{
    "require": {
        "elibyy/tcpdf-laravel": "5.2.*"
    }
}

Or use directly composer in the console : 

composer require elibyy/tcpdf-laravel

The installation will take a while as the TCPDF library is big.

Implementation

Now that the library is included in your project, you need to enable it in your service provider.

Go to your app.php file (yourapp/config/app.php) and add the Laravel TCPDF service provider :

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

Now you'll be able to use TCPDF in your project.

Using TCPDF with an alias

You can define an alias (whatever you want) for TCPDF to prevent the direct declaration of a new class element and use the TCPDF::Method syntax without instantiation.

Go to your app.php file (yourapp/config/app.php) and add the alias for the class :

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

Now, TCPDF has an alias as PDF and you'll be able to generate PDF's in your controller using :

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use PDF;

class DefaultController extends Controller
{
    public function index()
    {
        $html = '<h1>Hello World</h1>';
        
        PDF::SetTitle('Hello World');
        PDF::AddPage();
        PDF::writeHTML($html, true, false, true, false, '');

        PDF::Output('hello_world.pdf');
    }
}

And now you can generate awesome PDF from html using TCPDF.

Note: as you don't create a new instance of the TCPDF class when you use an alias, if you want to create multiple PDF's you need to use the reset method to create a new one.

To create multiple PDF's use the PDF::reset method to work on a new PDF instance :

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use PDF;

class DefaultController extends Controller
{
    public function index()
    {    
        for ($i = 0; $i < 5; $i++) {
            $html = '<h1>Hello World '.$i.'</h1>';
            PDF::SetTitle('Hello World'.$i);
            PDF::AddPage();
            PDF::Write(0, 'Hello World'.$i);
            // Write the file instead of throw it in the browser
            PDF::Output(public_path('hello_world' . $i . '.pdf'), 'F');
            PDF::reset();
        }
    }
}

Using TCPDF without an alias

Without an alias, we'll need to declare an instance of the TCPDF class, just add an use statement to the Elibyy\TCPDF\Facades\TCPDF class in the top of your controller.

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Elibyy\TCPDF\Facades\TCPDF;

class DefaultController extends Controller
{
    public function index()
    {
        $html = '<h1>Hello world</h1>';
        $pdf = new TCPDF();
        $pdf::SetTitle('Hello World');
        $pdf::AddPage();
        $pdf::writeHTML($html, true, false, true, false, '');
        $pdf::Output('hello_world.pdf');
    }
}

Recommendations

Is a good practice to handle the HTML content in a view instead of manipulate it manually in the controller.

You can draw your PDF content as html in a template, retrieve the html generated by the view and write into the PDF :

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Elibyy\TCPDF\Facades\TCPDF;

class DefaultController extends Controller
{
    public function index()
    {
        $view = \View::make('myview_name');
        $html = $view->render();
        
        $pdf = new TCPDF();
        $pdf::SetTitle('Hello World');
        $pdf::AddPage();
        $pdf::writeHTML($html, true, false, true, false, '');
        $pdf::Output('hello_world.pdf');
    }
}

Have fun !

Become a more social person