Laravel 9 Generate PDF From HTML Using TCPDF

Websolutionstuff | Jan-31-2023 | Categories : Laravel PHP

In this article, we will see laravel 9 generate PDF from HTML using TCPDF. Here, we will learn how to integrate tcpdf in laravel 7, laravel 8, and laravel 9. TCPDF is a service provider with a basic configuration. Also, TCPDF is not supported in PHP 7. So, we will generate PDF from HTML using TCPDF.

The Laravel TCPDF service provider can be installed via composer by requiring the elibyy/tcpdf-laravel package in your project's composer.json. Using TCPDF you can set a custom title, page, text, etc.

So, let's see how to generate PDF from HTML with TCPDF in laravel 7/8/9, how to use TCPDF in laravel 9, and how to integrate TCPDF in laravel 9.

Step 1: Install Laravel 9

In this step, we will install laravel 9 using the following command.

composer create-project laravel/laravel laravel9_tcpdf_example

 

Step 2: Install elibyy/tcpdf-laravel package

Now, we will install elibyy/tcpdf-laravel package using the composer command.

composer require elibyy/tcpdf-laravel

or

Laravel 5.5+ will use the auto-discovery function.

{
    "require": {
        "elibyy/tcpdf-laravel": "^9.0"
    }
}

If you don't use auto-discovery you will need to include the service provider/facade in config/app.php.

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

//...

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

Please note: TCPDF cannot be used as an alias

 

 

Step 3: Create Route

In this step, we will create routes in the web.php file.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\TCPDFController;
  
/*
|--------------------------------------------------------------------------
| 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('tcpdf', [TCPDFController::class,'generateTCPDF']);

 

Step 4: Create Controller

Now, we will create a TCPDFController.php file. So, add the following code to that file.

app/Http/Controllers/TCPDFController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Elibyy\TCPDF\Facades\TCPDF;
  
class TCPDFController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function generateTCPDF(Request $request)
    {
        $filename = 'tcpdf_example.pdf';
  
        $data = [
            'title' => 'How to generate PDF using TCPDF in Laravel 7/8/9 - Websolutionstuff'
        ];
  
        $html = view()->make('tcpdf_example', $data)->render();
  
        $pdf = new TCPDF;
          
        $pdf::SetTitle('PDF Example');
        $pdf::AddPage();
        $pdf::writeHTML($html, true, false, true, false, '');
  
        $pdf::Output(public_path($filename), 'F');
  
        return response()->download(public_path($filename));
    }
}

 

 

Step 5: Create Blade File

In this step, we will create a tcpdf_example.blade.php file. So, add the following code to that file.

resources/views/tcpdf_example.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 9 Generate PDF From HTML Using TCPDF - Websolutionstuff</title>
</head>
<body>
    <h3 style="color:red;">{!! $title !!}</h3>
 
    <br>
  
    <p>Thanks and Regards</p>
</body>
</html>

 

Step 6: Run Laravel 9 Application

Now, we will run the laravel 9 TCPDF example using the following command.

php artisan serve

 


You might also like:

Recommended Post
Featured Post
How to Add and Delete Rows Dynamically using jQuery
How to Add and Delete Rows Dyn...

In this article, we will see how to add and delete rows dynamically using jquery. Also, we will see without&nb...

Read More

Jan-02-2021

How to Convert PDF to Image in Laravel 10
How to Convert PDF to Image in...

Greetings, Laravel enthusiasts! Today, let's unravel a common challenge in web development – converting PDFs t...

Read More

Dec-22-2023

How to Search Object by ID and Remove It from JSON Array In JavaScript
How to Search Object by ID and...

In this example we will see how to search object by id and remove it from json array in javascript. we need to add ...

Read More

May-26-2021

Laravel tips: DB Models and Eloquent - 2023
Laravel tips: DB Models and El...

Welcome to the fourth installment of our "Laravel Tips: DB Models and Eloquent" series. In this article, we co...

Read More

Oct-18-2023