How To Generate PDF Using Laravel TCPDF Library Tutorial

Share this Article
Reading Time: 4 minutes
279 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

Learn More –

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.

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

Open project to terminal and type the command to start development server

$ php artisan serve

URL: http://127.0.0.1:8000/pdf

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.

Buy Me a Coffee

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.

Was this post helpful?

PHP Web Development Courses on Offer in just $4
PHP Web Development Courses on Offer in just $4