CodeIgniter 4 PDF Tutorial: Generate PDF in CodeIgniter

Last Updated on by in CodeIgniter
Ideally, PDF is used to read the information in a document form. The PDF file is denoted by .pdf file extension and means as Portable Document Format.A PDF document manifests information for eBooks, application forms, user manuals, other documents.

Suppose you are a CodeIgniter developer and want to know how to create a PDF file from the HTML view template using the domPDF library in CodeIgniter 4.

To create coherence with your users, you send relevant information regularly.

Codeigniter can impetus this task with a domPDF package. We will help you out with how to integrate domPDF plugin in Codeigniter, especially when you are a beginner in CI development.

Many newbie developers get stuck when it comes to HTML to pdf conversion. Rest assured, we are going to make it easy for you.

At the end of this tutorial, you can generate a PDF file from the HTML layout with the DomPDF library.

The dompdf is an HTML to PDF converter.

At its heart, dompdf is (mostly) a CSS 2.1 compliant HTML layout and rendering engine written in PHP. It is a style-driven renderer:

it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes.

DomPDF library can be downloaded with a composer package and its impetus the PDF customization process.

You can use inline styling, font color, font size, and you can also include external stylesheet with it. Cool, isn’t it?

Install Codeigniter Application

We are going to install the CodeIgniter 4 application using composer package.

composer create-project codeigniter4/appstarter

Rename the Codeigniter folder name, as we have changed to codeigniter-pdf-example.

Go inside the project folder.

cd codeigniter-pdf-example

Activate Errors Reporting

To deal with the errors, we have to turn on Codeigniter’s failure, which are turned off by default.

Go to app/Config/Boot/development.php and set the display_errors to 1 instead of 0. Do the exact same thing in app/Config/Boot/production.php.

ini_set('display_errors', '1');

Install & Configure DomPDF

In this step we are going to install DomPDF plugin using Composer package. Run the below command to install Composer plugin.

composer require dompdf/dompdf

Once we are done installing the PDF package in Codeigniter application. Then, go to 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',

Create PDF Controller

Create two functions, one for showing the tables in the view. Second method helps to convert HTML into PDF. Generate a PdfController.php file in app/Controllers folder. Then, place the following code inside of 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');


Define Route

We have to create a route that renders the table into the view, place the following code in app/Config/Routes.php file.

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

$routes->get('/', 'PdfController::index');
$routes->match(['get', 'post'], 'PdfController/htmlToPDF', 'PdfController::htmlToPDF');

Create View

Ultimately, we have to create a pdf_view.php file that we will use to convert HTML to PDF.

Place the following code inside the app/Views/pdf_view.php 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 Example -</title>
  <link rel="stylesheet" href="">

  <div class="container mt-5">

    <h2>Generate PDF in Codeigniter from View</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>


Start Application

Run the following command to start the application:

php spark serve

Enter the following URL in the browser to convert the HTML into PDF in Codeigniter application:


Download the complete code of this tutorial from GitHub.

Digamber - Author

Hi, I'm Digamber Singh, a New Delhi-based full-stack developer, tech author, and open-source contributor with 10+ years' experience in HTML, CSS, JavaScript, PHP, and WordPress.