Codeigniter 4 PDF Generator Tutorial Example

Codeigniter 4 PDF Generator Tutorial Example

How to generate dynamic pdf using dompdf in Codeigniter 4. In this example, you will learn how to generate dynamic pdf using dompdf in Codeigniter 4 and as well as download it.

If you are working on codeigniter 4 app and at that time, you may want to generate pdf file from views in codeigniter 4 app. Or you are searching how to create a PDF file from the HTML view template using the domPDF library in Codeigniter 4.

So, This Codeigniter 4 ajax dependent dropdown example will guide you step by step on how to generate or create and download dynamic pdf using dompdf in Codeigniter.

How 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

Step 1: Download Codeigniter Project

In this step, you will download the latest version of Codeigniter 4, Go to this link Download Codeigniter 4 fresh new setup and unzip the setup in your local system xampp/htdocs/ . And change the download folder name “demo”

Step 2: Basic Configurations

Next, you will set some basic configuration on the app/config/app.php file, so let’s go to application/config/config.php and open this file on text editor.

Set Base URL like this

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

Step 3: Setup Database Credentials

In this step, you need to connect our project to the database. you need to go app/Config/Database.php and open database.php file in text editor. After opening the file in a text editor, you need to set up database credentials in this file like below.

	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,

Step 4: Install Dompdf Library

In this step, install DomPDF plugin using Composer package. So open your terminal and execute the following command on it:

composer require dompdf/dompdf

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

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

Step 5: Create Controller

In this step, Visit app/Controllers and create a controller name PdfController.php. In this controller, you need to add the following methods into it:

namespace App\Controllers;
use CodeIgniter\Controller;

class PdfController extends Controller

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

    function htmlToPDF(){
        $dompdf = new \Dompdf\Dompdf(); 
        $dompdf->setPaper('A4', 'landscape');


Step 6: Create View

In this step, you need to create one view files name pdf-view.php and update the following code into your file:

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Codeigniter 4 PDF Generate Example -</title>
<link rel="stylesheet" href="">
<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
<table class="table table-striped table-hover mt-4">
<td>Airi Satou</td>
<td>Angelica Ramos</td>
<td>Chief Executive Officer (CEO)</td>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>Bradley Greer</td>
<td>Software Engineer</td>
<td>Brenden Wagner</td>
<td>Software Engineer</td>
<td>San Francisco</td>
<td>Brielle Williamson</td>
<td>Integration Specialist</td>
<td>New York</td>
<td>Bruno Nash</td>
<td>Software Engineer</td>
<td>Caesar Vance</td>
<td>Pre-Sales Support</td>
<td>New York</td>
<td>Cara Stevens</td>
<td>Sales Assistant</td>
<td>New York</td>
<td>Cedric Kelly</td>
<td>Senior Javascript Developer</td>

Step 7: Create Route

In this step, you need to create a route that renders the table into the view, place the following code in app/Config/Routes.php file.

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

Step 8: Start Development Server

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

php spark serve

Then, Go to the browser and hit below the URL:



In this Codeigniter 4 generate pdf from views using dompdf library example. In this tutorial you have successfully generated pdf file from view using dompdf library in codeigniter 4.

Recommende CodeIgniter 4 Tutorial

  1. Install / Download Codeigniter 4 By Manual, Composer, Git
  2. Codeigniter 4 Remove Public and Index.php From URL
  3. Codeigniter 4 Application Folder / Directory Structure
  4. Create Controller, Model, View Example Codeigniter 4

If you have any questions or thoughts to share, use the comment form below to reach us.


My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *