Codeigniter 4 PDF Generator Tutorial Example

Codeigniter 4 PDF Generator Tutorial Example

Codeigniter tutorial, you will learn how to create a PDF file from an HTML view template using the domPDF library in Codeigniter 4. This is helpful if you’re working on a Codeigniter 4 app and want to generate a PDF file from views.

First, you need to install the domPDF library in Codeigniter 4. Then, you can use it to generate a dynamic PDF and download it. This tutorial will guide you through the steps of generating and downloading a dynamic PDF using domPDF in Codeigniter 4.

How to Generate PDF File in Codeigniter 4

Follow the following steps to generate pdf file in codeigniter 4:

  • Download Codeigniter Latest

  • Basic Configurations

  • Setup Database Credentials

  • Install Dompdf Library

  • Create Controller

  • Create View

  • Create Route

  • Start Development Server

  • Conclusion

Download Codeigniter Latest

To get started with CodeIgniter 4, the first step is to download the latest version of the framework. You can download the framework from the official CodeIgniter website at https://codeigniter.com/download.

Once you have downloaded the CodeIgniter 4 setup, unzip the archive and save it in your local system. You can save it in the XAMPP/htdocs/ directory if you are using XAMPP as your local development environment.

Basic Configurations

Now, we need to configure some basic settings in the “app/config/app.php” file. To do this, we will open the “config.php” file located in the “application/config” folder using a text editor.

Set Base Url Like This

public $baseURL = 'http://localhost:8080';
To
public $baseURL = 'http://localhost/demo/';

Setup Database Credentials

Now, we need to connect our CodeIgniter 4 project to the database. To do this, we will open the “database.php” file located in the “app/Config” folder using a text editor.

public $default = [
    'DSN'      => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'demo',
    'DBDriver' => 'MySQLi',
    'DBPrefix' => '',
    'pConnect' => false,
    'DBDebug'  => (ENVIRONMENT !== 'production'),
    'cacheOn'  => false,
    'cacheDir' => '',
    'charset'  => 'utf8',
    'DBCollat' => 'utf8_general_ci',
    'swapPre'  => '',
    'encrypt'  => false,
    'compress' => false,
    'strictOn' => false,
    'failover' => [],
    'port'     => 3306,
];

Install Dompdf Library

To install the DomPDF plugin, you can use a tool called Composer package. To do this, you need to open your terminal and type in the following command:

composer require dompdf/dompdf

Next, you need to go to the app/Config/Autoload.php file and look for the $psr4 array. Here, you will need to add the dompdf service so that it can be registered.

public $psr4 = [
    APP_NAMESPACE => APPPATH, // For custom app namespace
    'Config'      => APPPATH . 'Config',
    'Dompdf'      => APPPATH . 'ThirdParty/dompdf/src',
];

Create Controller

Now, go to the app/Controllers directory and create a new controller called PdfController.php. Inside this controller, you’ll need to add the following methods:

<?php 
namespace App\Controllers;
use CodeIgniter\Controller;

class PdfController extends Controller
{

    public function index() 
    {
        return view('pdf_view');
    }

    function htmlToPDF(){
        $dompdf = new \Dompdf\Dompdf(); 
        $dompdf->loadHtml(view('pdf_view'));
        $dompdf->setPaper('A4', 'landscape');
        $dompdf->render();
        $dompdf->stream();
    }

}

Create Views

Next, create a new view file called pdf-view.php. To do this, you can open your text editor and save a new file with that name. Then, add the following code to the file:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
        <title>Codeigniter 4 PDF Generate Example - torqueprogramming.co.in</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" />
    </head>
    <body>
        <div class="container mt-5">
            <h2>Codeigniter 4 Generate PDF From View using DOMPdf</h2>
            <div class="d-flex flex-row-reverse bd-highlight">
                <a href="<?php echo base_url('PdfController/htmlToPDF') ?>" class="btn btn-primary">
                    Download PDF
                </a>
            </div>
            <table class="table table-striped table-hover mt-4">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Profile</th>
                        <th>City</th>
                        <th>Date</th>
                        <th>CTC</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Airi Satou</td>
                        <td>Accountant</td>
                        <td>Tokyo</td>
                        <td>33</td>
                        <td>2008/11/28</td>
                        <td>$162,700</td>
                    </tr>
                    <tr>
                        <td>Angelica Ramos</td>
                        <td>Chief Executive Officer (CEO)</td>
                        <td>London</td>
                        <td>47</td>
                        <td>2009/10/09</td>
                        <td>$1,200,000</td>
                    </tr>
                    <tr>
                        <td>Ashton Cox</td>
                        <td>Junior Technical Author</td>
                        <td>San Francisco</td>
                        <td>66</td>
                        <td>2009/01/12</td>
                        <td>$86,000</td>
                    </tr>
                    <tr>
                        <td>Bradley Greer</td>
                        <td>Software Engineer</td>
                        <td>London</td>
                        <td>41</td>
                        <td>2012/10/13</td>
                        <td>$132,000</td>
                    </tr>
                    <tr>
                        <td>Brenden Wagner</td>
                        <td>Software Engineer</td>
                        <td>San Francisco</td>
                        <td>28</td>
                        <td>2011/06/07</td>
                        <td>$206,850</td>
                    </tr>
                    <tr>
                        <td>Brielle Williamson</td>
                        <td>Integration Specialist</td>
                        <td>New York</td>
                        <td>61</td>
                        <td>2012/12/02</td>
                        <td>$372,000</td>
                    </tr>
                    <tr>
                        <td>Bruno Nash</td>
                        <td>Software Engineer</td>
                        <td>London</td>
                        <td>38</td>
                        <td>2011/05/03</td>
                        <td>$163,500</td>
                    </tr>
                    <tr>
                        <td>Caesar Vance</td>
                        <td>Pre-Sales Support</td>
                        <td>New York</td>
                        <td>21</td>
                        <td>2011/12/12</td>
                        <td>$106,450</td>
                    </tr>
                    <tr>
                        <td>Cara Stevens</td>
                        <td>Sales Assistant</td>
                        <td>New York</td>
                        <td>46</td>
                        <td>2011/12/06</td>
                        <td>$145,600</td>
                    </tr>
                    <tr>
                        <td>Cedric Kelly</td>
                        <td>Senior Javascript Developer</td>
                        <td>Edinburgh</td>
                        <td>22</td>
                        <td>2012/03/29</td>
                        <td>$433,060</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </body>
</html>

Create Route

In this step, you’ll need to create a new route that will display the table in the view. To do this, go to the app/Config/Routes.php file and add the following code:

$routes->get('/', 'PdfController::index');

Start Development Server

In this step, open your terminal and execute the following command to start development sever:

php spark serve