How to Generate PDF File in Codeigniter 4?

Apr 21, 2022 . Admin

Hi Guys,

This tutorial will give you an example of the Codeigniter 4 pdf tutorial. I explained simply a step-by-step Codeigniter 4 pdf tutorial example. you'll learn Codeigniter 4 pdf example. Here you will learn the Codeigniter 4 pdf tutorial: generate pdf in Codeigniter. Follow below tutorial step for generating pdf in Codeigniter.

In this example, I will learn you how to create a pdf in codeigniter can easy and simply create a 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 4

This is optional; however, if you have not created the codeigniter app, then you maygo ahead and execute the below command:

composer create-project codeigniter4/appstarter ci-news
Step 2: Install & Configure DomPDF

In this step we will require 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 third step,you will create to pdfController in app/Controllers folder.

        $dompdf->setPaper('A4', 'landscape');

Step 4: Define Route

We have to engender 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 4 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">

Step 6 : Run Codeigniter App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Codeigniter app:

php spark serve
Now, Go to your web browser, type the given URL and view the app output:

It will help you...

#Codeigniter 4