DEV Community

Cover image for How to Generate Pdf in PHP CodeIgniter 4 using *dompdf*
TAIO Sylvain
TAIO Sylvain

Posted on

How to Generate Pdf in PHP CodeIgniter 4 using *dompdf*

Step 1: Create the Database Table
Create a users table in your MySQL database:

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    surname VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL UNIQUE,
    university VARCHAR(100),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the User Model
In your CodeIgniter project, create a model named UserModel.php in app/Models/:

<?php

namespace App\Models;

use CodeIgniter\Model;

class UserModel extends Model
{
    protected $table = 'users';
    protected $primaryKey = 'id';

    protected $allowedFields = ['name', 'surname', 'email', 'university'];
    protected $useTimestamps = true;
    protected $createdField = 'created_at';
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Install DOMPDF
Run the following command in your project directory to install DOMPDF:

composer require dompdf/dompdf
Enter fullscreen mode Exit fullscreen mode

Step 4: Create the PDF Generation Controller
Create a controller named PdfController.php in app/Controllers/:

<?php

namespace App\Controllers;
use App\Models\UserModel;
use Dompdf\Dompdf;
use Dompdf\Options;

class PdfController extends BaseController
{

    public function generateUserListPdf(){
        $userModel = new UserModel();

        // Retrieve all users from the database
        $users = $userModel->findAll();
        $options = new Options();
        $options->set('isRemoteEnable',true);

        $dompdf = new Dompdf($options);

        $html = view('user_list_pdf',['users' => $users]);

        $dompdf->loadHtml($html);

        $dompdf->render();

        $filename = 'user_list_'.date('YmdHis').'pdf';

        $dompdf->stream($filename,['Attachment'=>false]);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Create the HTML View for the PDF
In app/Views/, create a new file named user_list_pdf.php with the following content:

<!DOCTYPE html>
<html>
<head>
    <style>
        table {
            border-collapse: collapse;
            width: 100%;
        }
        th, td {
            border: 1px solid black;
            padding: 8px;
        }
        th {
            background-color: lightgray;
        }
    </style>
</head>
<body>
    <h1>List of Users</h1>
    <table>
        <tr>
            <th>No</th>
            <th>Name and Surname</th>
            <th>Email</th>
            <th>University</th>
            <th>Created Date</th>
        </tr>
        <?php foreach ($users as $index => $user) : ?>
            <tr>
                <td><?= $index + 1; ?></td>
                <td><?= esc($user['name']); ?> <?= esc($user['surname']); ?></td>
                <td><?= esc($user['email']); ?></td>
                <td><?= esc($user['university']); ?></td>
                <td><?= esc($user['created_at']); ?></td>
            </tr>
        <?php endforeach; ?>
    </table>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Step 6: Define the Route
In app/Config/Routes.php, add a route to access the PDF generation function:

$routes->get('generate-user-list-pdf', 'PdfController::generateUserListPdf');
Enter fullscreen mode Exit fullscreen mode

Step 7: Test the Setup
Visit http://yourdomain.com/generate-user-list-pdf in your browser. This should:

Retrieve all users from the database.
Render the user_list_pdf view with user data.
Generate a PDF with the user list and display it in the browser.

Top comments (0)