Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Codeigniter 4 Generate PDF Example Tutorial

Codeigniter 4 Generate PDF Example Tutorial

Posted on April 10, 2020December 16, 2022 By XpertPhp No Comments on Codeigniter 4 Generate PDF Example Tutorial

In this article, we will explain to you how to generate PDF in CodeIgniter 4(Codeigniter 4 Generate PDF Example Tutorial). we can easily generate the pdf file in CodeIgniter 4.

In this example, we will use the Dompdf package library to create a pdf in Codeigniter. we can also Generate Dynamically HTML to PDF File using dompdf in Codeigniter 4. so you can see our following example.

Overview
Step 1: Download Codeigniter
Step 2: Basic Configurations
Step 3: Create a Database in table
Step 4: Connect to Database
Step 5: Install DOMPdf Package
Step 6: Create Controller and Model
Step 7: Create Views Files
Step 8: Run The Application

Step 1: Download Codeigniter
If you want to download or install the latest version of CodeIgniter 4 then you can go to Codeigniter’s official site and download the latest version of Codeigniter 4. after the downloaded you can configure in “xampp/htdocs/” directory.

Step 2: Basic Configurations
If you want to Basic Configurations in your project then you can below Url.
Codeigniter 4 Removing Index.Php From Url

Step 3: Create a Database in table
In this step, We will create the database and table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CREATE TABLE IF NOT EXISTS `students` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(64) NOT NULL,
  `last_name` varchar(64) NOT NULL,
  `address` text NOT NULL,
  `email` varchar(64) NOT NULL,
  `mobile` varchar(12) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
 
INSERT INTO `students` (`id`, `first_name`, `last_name`, `address`, `email`, `mobile`) VALUES
(1, 'john', 'doe', 'USA', 'jd@gmail.com', '787878787'),
(2, 'kal', 'mark', 'CANADA', 'km@gmail.com', '7887878787'),
(3, 'James', 'Mary', 'USA', 'jm@gmail.com', '4364373434'),
(4, 'William', 'Elizabeth', 'USA', 'we@gmail.com', '7878787'),
(5, 'Matthew', 'Betty', 'USA', 'mb@gmail.com', '898989'),
(6, 'Anthony', 'Margaret', 'INDIA', 'am@gmail.com', '8989898989'),
(7, 'Donald', 'Ashley', 'CANADA', 'da@gmail.com', '3343434345'),
(8, 'Kevin', 'Carol', 'USA', 'kc@gmail.com', '2223334445');

Step 4: Connect to Database
Go to the “app/Config/Database.php” folder and open the database.php file some changes in this file like hostname, database username, database password, and database name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public $default = [
'DSN'      => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'codeigniter4_pagination',
'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,
];

Step 5: Install DOMPdf Package
In this step, We will install the dompdf package vie composer for the generate pdf in codeigniter 4 application. so you can follow our composer command.

1
composer require dompdf/dompdf

After that, open the app/Config/Autoload.php file and look for the $psr4 array, here you have to register the dompdf service.

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

Step 6: Create Controller and Model
In this step, we will create the “Student.php” controller and the “StudentModel.php” model.
app/Controllers/Student.php

See also  Codeigniter 4 cURL POST Request Example Tutorial
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
 
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\StudentModel;
class Student extends Controller
{
    public function __construct()
    {
        
    }
    public function index()
    {  
$model = new StudentModel();
        $data = [
            'students' => $model->orderBy('id', 'DESC')->findAll()
        ];
        
        return view('list', $data);
    }
    function convertToPdf(){
$model = new StudentModel();
$data = $model->orderBy('id', 'DESC')->findAll();
        $dompdf = new \Dompdf\Dompdf();
// Sending data to view file
        $dompdf->loadHtml(view('list', ["students" => $data]));
        $dompdf->setPaper('A4', 'landscape');
        $dompdf->render();
        $dompdf->stream();
    }  
}
 
?>

app/Models/StudentModel.php

1
2
3
4
5
6
7
8
9
10
11
12
<?php
namespace App\Models;
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;
class StudentModel extends Model
{
    protected $table = 'Students';
    protected $allowedFields = ['first_name','last_name','address','email', 'mobile'];
}
?>

Step 7: Create Views Files
Finally, we will create the list.php in the app/views directory.

app/views/list.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE html>
<html>
<head>
  <title>Codeigniter 4 Generate PDF Example Tutorial - XpertPhp</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-9">
<h2>List Student</h2>
</div>
<div class="col-md-3">
<a class="btn btn-dark" href="<?php echo site_url('student/convertToPdf') ?>">
Convert to PDF
</a>
</div>
</div>
<div class="row">
<div class="col-md-9">
<table class="table table-bordered">
<thead>
<tr>
   <th>Id</th>
   <th>First Name</th>
   <th>Last Name</th>
   <th>Email</th>
   <th>Mobile</th>
</tr>
</thead>
<tbody>
<?php if($students): ?>
<?php foreach($students as $student): ?>
<tr>
   <td><?php echo $student['id']; ?></td>
   <td><?php echo $student['first_name']; ?></td>
   <td><?php echo $student['last_name']; ?></td>
   <td><?php echo $student['email']; ?></td>
   <td><?php echo $student['mobile']; ?></td>
</tr>
   <?php endforeach; ?>
   <?php endif; ?>
</tbody>
   </table>
</div>
</div>
</div>
</body>
</html>

Step 8: Run The Application
We can start the server and run the codeigniter 4 application using the below command.

1
php spark serve

Now we will run our example using the below Url in the browser.

1
http://localhost:8080/student

If you liked this article, you can also download it through our Github Repository.

Codeigniter Tags:Codeigniter 4 Tutorial

Post navigation

Previous Post: CodeIgniter 4 Rest Api Example Tutorial
Next Post: codeigniter 4 multiple images & files upload example

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Angular 15 CRUD Application Example Tutorial
  • Laravel 10 Form Validation Example Tutorial
  • Angular 15 Custom Form Validation Example
  • Laravel 10 Send Email Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com