Laravel Generate HTML To PDF By Using Elibyy/tcpdf-laravel Package

We'll talk about a Laravel TCPDF example in this tutorial. We'll have a look at a tcpdf integration in the Laravel example. This article gives a straightforward example of how to use tcpdf in Laravel. I'd like to demonstrate how to create a PDF file with Laravel Tcpdf.


This example will work from Laravel versions 6 - 9.


To create a PDF file in Laravel, we'll utilize the elibyy/tcpdf-laravel composer package. To create a PDF file with Laravel TcPDF, we'll use the SetTitle(), AddPage(), writeHTML(), and Output() methods. So let's follow the below steps:


Step 1: Install Laravel

Using the command below, we must first create a new Laravel application. Run the following command after opening your terminal or command prompt:


composer create-project laravel/laravel html-to-pdf


Step 2: Install the elibyy/tcpdf-laravel package

To produce PDF files in Laravel, we will install the elibyy/tcpdf-laravel package here. Let's execute the following commands:


composer require elibyy/tcpdf-laravel


Step 3: Make the Controller

At this stage, we will have to construct a PDFController that contains an index() method.

In the controller file, add the code shown below.


app/Http/Controllers/PDFController.php

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use Elibyy\TCPDF\Facades\TCPDF;
 
class PDFController extends Controller
{
    /**
    * Write code on Method
    *
    * @return response()
    */
    public function index(Request $request)
    {
        $filename = 'html-to-pdf.pdf';
 
        $data = [
            'title' => 'Generating HTML to PDF by using TCPDF'
        ];
 
        $html = view()->make('pdfSample', $data)->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));
    }
}


Step 4: Create View File.

To generate an HTML to PDF file, we will make a file called pdfSample.blade.php. Hence, let's make the following changes to the blade file.


resources/views/pdfSample.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>'Generating HTML to PDF by using TCPDF</title>
</head>
<body>
    <h1 style="color:red;">{!! $title !!}</h1>

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


Step 5: Define Route

At this point, we need to define routes in Laravel that would generate a PDF file by utilizing TCPDF. Let's start by including the route below in the web.php file.


routes/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('pdf', [PDFController::class,'index']);


After completing all of the necessary steps, you will need to run the Laravel application by typing the following command and then pressing the enter key:


php artisan serve


Now, Open up your web browser, enter the following URL, and take a look at the output of the app:


http://localhost:8000/pdf