Codeigniter 4 Generate HTML to PDF Example Tutorial

Do you know how to export HTML to PDF files in Codeigniter 4 application? Well, if you don’t know this, then rest assured you are at the right place.

Through the help of this comprehensive Codeigniter PDF generator tutorial, we will teach you how to convert HTML to PDF in PHP CodeIgniter using the Dompdf package.

This eloquent guide helps you understand how to convert dynamic HTML to PDF, and we will dive into the Codeigniter domain and show you the best possible way to write elegant, effective PHP code.

This guide will explain how to generate PDF from View using DomPDF; like any other pragmatic tutorial, we will talk about all the fundamentals, controller, routes, views, functions, and nuances regarding installing and setting up the DomPdf library in Codeigniter app.

How to Dynamically Generate HTML to PDF File in Codeigniter 4

  • Step 1: Create Codeigniter Project
  • Step 2: Connect CI to Database
  • Step 3: Install DOMPdf Package
  • Step 4: Register Route
  • Step 5: Set Up Controller File
  • Step 6: Create and Set Up View File
  • Step 7: Run App in Browser

Create Codeigniter Project

The following url will land you on the official site, where you can download the latest version of the Codeigniter app.

https://www.codeigniter.com/download

Another method to download the app is by using the Composer command.

composer create-project codeigniter4/appstarter

Connect CI to Database

In order to connect the app to the database, you have to update the database credentials into the app/Config/Database.php file.

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

Install DOMPdf Package

It is easy to install the dompdf library; you can type the given composer command to add the DomPDF package into the Codeigniter app.

composer require dompdf/dompdf

Afterward, step inside the app/Config/Autoload.php file, look for the public $psr4 array, without further ado, define the Dompdf service into it.

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

Register Route

In this step, you have to produce a route that administers the table data into the view file.

Go ahead and insert the given code in the app/Config/Routes.php file.

/*
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
*/

$routes->setDefaultController('ClientController');
$routes->get('/html-to-pdf', 'ClientController::index');

Set Up Controller File

Create a brand new controller template inside the app/Controllers directory; afterwards, add the suggested code in the app/Controllers/ClientController.php.

<?php

namespace App\Controllers;
use CodeIgniter\Controller;



class ClientController extends Controller {

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

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

} ?>

Create and Set Up View File

In the final section, you need to go to the views folder; in the view folder, create an index.php file.

In this file, you have to create an HTML table, add a button on top of the table, tie it with the controller function to convert HTML to PDF file.

Add the provided code in app/Views/index.php file.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Codeigniter 4 PDF Generator Example</title>
</head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<body>
    <div class="container">
        <a class="btn btn-dark" href="<?php echo base_url('ClientController/convertToPdf') ?>">
            Convert to PDF
        </a>
        <table class="table">
            <thead>
                <tr>
                    <th scope="col">#</th>
                    <th scope="col">Name</th>
                    <th scope="col">Email</th>
                    <th scope="col">Date Created</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <th scope="row">1</th>
                    <td>Hunter George</td>
                    <td>[email protected]</td>
                    <td>1949-10-6</td>
                </tr>
                <tr>
                    <th scope="row">2</th>
                    <td>Kyle Johnson</td>
                    <td>[email protected]</td>
                    <td>1980-8-12</td>
                </tr>
                <tr>
                    <th scope="row">3</th>
                    <td>Pat Vang</td>
                    <td>[email protected]</td>
                    <td>1961-10-10</td>
                </tr>
                <tr>
                    <th scope="row">4</th>
                    <td>Brad Bailey</td>
                    <td>[email protected]</td>
                    <td>1992-3-31</td>
                </tr>
                <tr>
                    <th scope="row">5</th>
                    <td>Hunter George</td>
                    <td>[email protected]</td>
                    <td>1949-10-6</td>
                </tr>
                <tr>
                    <th scope="row">6</th>
                    <td>Kyle Johnson</td>
                    <td>[email protected]</td>
                    <td>1980-8-12</td>
                </tr>
                <tr>
                    <th scope="row">7</th>
                    <td>Pat Vang</td>
                    <td>[email protected]</td>
                    <td>1961-10-10</td>
                </tr>
                <tr>
                    <th scope="row">8</th>
                    <td>Brad Bailey</td>
                    <td>[email protected]</td>
                    <td>1992-3-31</td>
                </tr>
                <tr>
                    <th scope="row">9</th>
                    <td>Hunter George</td>
                    <td>[email protected]</td>
                    <td>1949-10-6</td>
                </tr>
                <tr>
                    <th scope="row">10</th>
                    <td>Kyle Johnson</td>
                    <td>[email protected]</td>
                    <td>1980-8-12</td>
                </tr>
                <tr>
                    <th scope="row">11</th>
                    <td>Pat Vang</td>
                    <td>[email protected]</td>
                    <td>1961-10-10</td>
                </tr>
                <tr>
                    <th scope="row">12</th>
                    <td>Brad Bailey</td>
                    <td>[email protected]</td>
                    <td>1992-3-31</td>
                </tr>
                <tr>
                    <th scope="row">13</th>
                    <td>Hunter George</td>
                    <td>[email protected]</td>
                    <td>1949-10-6</td>
                </tr>
                <tr>
                    <th scope="row">14</th>
                    <td>Kyle Johnson</td>
                    <td>[email protected]</td>
                    <td>1980-8-12</td>
                </tr>
                <tr>
                    <th scope="row">15</th>
                    <td>Pat Vang</td>
                    <td>[email protected]</td>
                    <td>1961-10-10</td>
                </tr>
            </tbody>
        </table>
    </div>
</body>
</html>

Run App in Browser

This final step shows you how to start the Codeigniter development server, execute the provided command.

Use the suggested url to test the load of more data on page scroll functionality.

php spark serve
http://localhost:8080/html-to-pdf

Summary

We have successfully integrated the DomPdf plugin in Codeigniter and learned how to export HTML to PDF in Codeigniter profoundly. We hope we have offered easy-to-follow explanations that will help you build the feature quickly.