Table of contents
In this post, I will show you an example of how to generate HTML to PDF using Laravel 9 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 converting it into PDF.
Â
Below I will show you a set of examples of how to implement Laravel with the 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 controllers 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:
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 and 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 a blade file so I will name 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 :)
Read next