How to Generate Pdf in PHP CodeIgniter sing *dompdf*

Barbara Streisand
Release: 2024-11-05 21:17:02
Original
733 people have browsed it

How to Generate Pdf in PHP CodeIgniter sing *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
);
Copy after login

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';
}
Copy after login

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

composer require dompdf/dompdf
Copy after login

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]);
    }
}
Copy after login

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>
Copy after login

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');
Copy after login

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.

The above is the detailed content of How to Generate Pdf in PHP CodeIgniter sing *dompdf*. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!