Codeigniter 4 PDF Tutorial: Generate PDF in Codeigniter

Last updated on: by Digamber
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.

<?php 
namespace App\Controllers;
use CodeIgniter\Controller;
class PdfController extends Controller
{
    public function index() 
	{
        return view('pdf_view');
    }
    function htmlToPDF(){
        $dompdf = new \Dompdf\Dompdf(); 
        $dompdf->loadHtml(view('pdf_view'));
        $dompdf->setPaper('A4', 'landscape');
        $dompdf->render();
        $dompdf->stream();
    }
}

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">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Codeigniter 4 PDF Example - positronx.io</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
</head>
<body>
  <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
      </a>
    </div>
    <table class="table table-striped table-hover mt-4">
      <thead>
        <tr>
          <th>Name</th>
          <th>Profile</th>
          <th>City</th>
          <th>Date</th>
          <th>CTC</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Airi Satou</td>
          <td>Accountant</td>
          <td>Tokyo</td>
          <td>33</td>
          <td>2008/11/28</td>
          <td>$162,700</td>
        </tr>
        <tr>
          <td>Angelica Ramos</td>
          <td>Chief Executive Officer (CEO)</td>
          <td>London</td>
          <td>47</td>
          <td>2009/10/09</td>
          <td>$1,200,000</td>
        </tr>
        <tr>
          <td>Ashton Cox</td>
          <td>Junior Technical Author</td>
          <td>San Francisco</td>
          <td>66</td>
          <td>2009/01/12</td>
          <td>$86,000</td>
        </tr>
        <tr>
          <td>Bradley Greer</td>
          <td>Software Engineer</td>
          <td>London</td>
          <td>41</td>
          <td>2012/10/13</td>
          <td>$132,000</td>
        </tr>
        <tr>
          <td>Brenden Wagner</td>
          <td>Software Engineer</td>
          <td>San Francisco</td>
          <td>28</td>
          <td>2011/06/07</td>
          <td>$206,850</td>
        </tr>
        <tr>
          <td>Brielle Williamson</td>
          <td>Integration Specialist</td>
          <td>New York</td>
          <td>61</td>
          <td>2012/12/02</td>
          <td>$372,000</td>
        </tr>
        <tr>
          <td>Bruno Nash</td>
          <td>Software Engineer</td>
          <td>London</td>
          <td>38</td>
          <td>2011/05/03</td>
          <td>$163,500</td>
        </tr>
        <tr>
          <td>Caesar Vance</td>
          <td>Pre-Sales Support</td>
          <td>New York</td>
          <td>21</td>
          <td>2011/12/12</td>
          <td>$106,450</td>
        </tr>
        <tr>
          <td>Cara Stevens</td>
          <td>Sales Assistant</td>
          <td>New York</td>
          <td>46</td>
          <td>2011/12/06</td>
          <td>$145,600</td>
        </tr>
        <tr>
          <td>Cedric Kelly</td>
          <td>Senior Javascript Developer</td>
          <td>Edinburgh</td>
          <td>22</td>
          <td>2012/03/29</td>
          <td>$433,060</td>
        </tr>
      </tbody>
    </table>
  </div>
</body>
</html>

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:

http://localhost:8080

Download the complete code of this tutorial from GitHub.

positronX.io - Tamso Ma Jyotirgamaya
Digamber

A Full-stack developer with a passion to solve real world problems through functional programming.