DEV Community

Cover image for Laravel 8 TCPDF Tutorial Generate HTML to PDF
Code And Deploy
Code And Deploy

Posted on

Laravel 8 TCPDF Tutorial Generate HTML to PDF

Originally posted @ https://codeanddeploy.com visit and download the sample code:
https://codeanddeploy.com/blog/laravel/laravel-8-tcpdf-tutorial-generate-html-to-pdf

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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']);
Enter fullscreen mode Exit fullscreen mode

Run the command below to make a controller:

php artisan make:controller PdfController
Enter fullscreen mode Exit fullscreen mode

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');
    }
}
Enter fullscreen mode Exit fullscreen mode

Here is the result:

laravel-8-tcpdf

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));
    }
}
Enter fullscreen mode Exit fullscreen mode

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));
    }
}
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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));
    }
}
Enter fullscreen mode Exit fullscreen mode

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

For more details about this package visit it here.

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/laravel-8-tcpdf-tutorial-generate-html-to-pdf if you want to download this code.

Happy coding :)

Latest comments (0)