Table of contents

laravel tcpdf

In this post, I will show you an example how to generate HTML to PDF using Laravel 8 TCPDF. Usually PDF is useful if you need to generate a report from your application. And the easier way to do the layout is using HTML and convert it as PDF.

 

Below I will show you a set of example how to implement Laravel with TCPDF package.

 

Step 1: Laravel Installation

If you don't have a Laravel 8 install in your local just run the following command below:

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

cd laravel-tcpdf

 

Step 2: Install TCPDF Package

To generate HTML to PDF in Laravel we need to install elibyy/tcpdf-laravel package. Run the following command below:

composer require elibyy/tcpdf-laravel

 

Step 3: Setup Routes and Controller

Let's create routes and controller for our Laravel TCPDF generator.

 

routes.php/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PdfController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('pdf', [PdfController::class, 'index']);

 

Run the command below to make a controller:

php artisan make:controller PdfController

 

Step 4: Laravel TCPDF Example #1

Then edit the PdfController generated. See below:

<?php

namespace App\Http\Controllers;

use PDF;
use Illuminate\Http\Request;

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

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

 

Here is the result:

laravel tcpdf example

As you can see we generated our PDF with color so this is awesome because we can support basic styles of CSS.

 

Step 5: Download TCPDF Example

Now, let's try to download the PDF result above. We will save the PDF file to our public folder then download it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;

class PdfController extends Controller
{
    public function index() 
    {
    	$filename = 'hello_world.pdf';
    	$html = '<h1 style="color:red;">Hello World</h1>';
        
        PDF::SetTitle('Hello World');
        PDF::AddPage();
        PDF::writeHTML($html, true, false, true, false, '');

        PDF::Output(public_path($filename), 'F');

        return response()->download(public_path($filename));
    }
}

 

Step 6: TCPDF without an Alias

As we see above we use PDF as our alias. Now let's use the TCPDF class just add it to the above of your controller use Elibyy\TCPDF\Facades\TCPDF;

 

See below code example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Elibyy\TCPDF\Facades\TCPDF;

class PdfController extends Controller
{
    public function index() 
    {
    	$filename = 'hello_world.pdf';
    	$html = '<h1 style="color:red;">Hello World</h1>';

    	$pdf = new TCPDF;
        
        $pdf::SetTitle('Hello World');
        $pdf::AddPage();
        $pdf::writeHTML($html, true, false, true, false, '');

        $pdf::Output(public_path($filename), 'F');

        return response()->download(public_path($filename));
    }
}

 

Step 7: Render HTML from View

Better to render the HTML from view instead of doing it manually inside our controller.

 

First you need to create a folder in my case pdf inside resources/views/ then create blade file so I will named it sample.blade.php then edit the code see below example:

<!DOCTYPE html>
<html>
<head>
	<title>Generate Laravel TCPDF by codeanddeploy.com</title>
</head>
<body>
	<h1 style="color:red;">{!! $title !!}</h1>

	<br>

	<p>Your message here.</p>
</body>
</html>

 

Then edit your controller code. See below sample:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Elibyy\TCPDF\Facades\TCPDF;

class PdfController extends Controller
{
    public function index() 
    {
    	$filename = 'hello_world.pdf';

    	$data = [
    		'title' => 'Hello world!'
    	];

    	$view = \View::make('pdf.sample', $data);
        $html = $view->render();

    	$pdf = new TCPDF;
        
        $pdf::SetTitle('Hello World');
        $pdf::AddPage();
        $pdf::writeHTML($html, true, false, true, false, '');

        $pdf::Output(public_path($filename), 'F');

        return response()->download(public_path($filename));
    }
}

 

Now you have the basic already how do the Laravel with TCPDF.

 

For more details about this package visit it here.

 

I hope it helps :)