Codeigniter 4 PDF Tutorial

Oct 27, 2020



Hi Guys,

In this example,I will learn you how to create pdf in codeigniter can easy and simply create pdf in codeigniter 4.

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. In that case, this tutorial is good to go for you.

Step 1: Install Codeigniter Application

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

composer create-project codeigniter4/appstarter

Step 2: 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',


Step 3: Create PDF Controller

In this step,you will create to pdfController in app/Controllers folder.


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 4: 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.

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

Step 5: Create View

we have to create a pdf_view.php file that we will use to convert HTML to PDF. Place the following code inside the application/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">


































It will help you...