How To Generate PDF Using Laravel TCPDF Library Tutorial

Reading Time: 4 minutes
1,000 Views

Inside this article we will see the concept i.e How To Generate PDF Using Laravel TCPDF Library Tutorial. Article contains the classified information about generating PDF file using TCPDF composer package.

If you are looking for a solution i.e generate PDF file using TCPDF library in laravel application then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

TCPDF is a free and open source software PHP class for generating PDF documents. TCPDF is the only PHP-based library that includes complete support for UTF-8 Unicode and right-to-left languages, including the bidirectional algorithm

Read More: Laravel How to Get All env Variables Example Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Install TCPDF Composer Library

Open project into terminal. Run this command to install TCPDF library.

$ composer require elibyy/tcpdf-laravel

When you install it will be like:

Next,

Create Controller

Open project into terminal and run this command.

$ php artisan make:controller PDFController

It will create a PDFController.php file inside /app/Http/Controllers folder.

Read More: How To Work with cURL DELETE Request in Codeigniter 4

Open file and write this code into it.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Elibyy\TCPDF\Facades\TCPDF;
  
class PDFController extends Controller
{
    public function index()
    {
        $filename = 'demo.pdf';
  
        $data = [
            'title' => 'How To Generate PDF Using Laravel TCPDF Library'
        ];
  
        $html = view()->make('pdf-file', $data)->render();
  
        $pdf = new TCPDF;
          
        $pdf::SetTitle('Generate PDF Using Laravel Library');
        $pdf::AddPage();
        $pdf::writeHTML($html, true, false, true, false, '');
  
        $pdf::Output(public_path($filename), 'F');
  
        return response()->download(public_path($filename));
    }
}

Create Blade Template File

Create a file pdf-file.blade.php inside /resources/views folder.

Open view file and write this code into it.

<!DOCTYPE html>
<html>
<head>
    <title>How To Generate PDF Using Laravel TCPDF Library</title>
</head>
<body>
    <h1 style="color:red;">{!! $title !!}</h1>
 
    <br>
  
    <p>Your message here.</p>
</body>
</html>
  

Add Route

Open web.php file from /routes folder. Add this route into it.

//...
use App\Http\Controllers\PDFController;

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

//...

Application Testing

Run this command into project terminal to start development server,

php artisan serve

URL: http://127.0.0.1:8000/pdf

Read More: How To Work with cURL PUT Request in Codeigniter 4

When we hit the above URL, it downloads a PDF file i.e demo.pdf and save into your local system.

We hope this article helped you to learn How To Generate PDF Using Laravel TCPDF Library Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more