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

app/Controllers/PdfController.php
loadHtml(view('pdf_view'));
        $dompdf->setPaper('A4', 'landscape');
        $dompdf->render();
        $dompdf->stream();
    }

}
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.

app/Config/Routes.php
$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">

<head>
  <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="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
</head>

<body>
    <div class="container mt-5">

        <h2>Generate PDF in Codeigniter 4 from View - Mywebtuts.com</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>City</th>
                    <th>Date</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Bhavesh</td>
                    <td>Halvad</td>
                    <td>22/10/2020</td>
                </tr>
                <tr>
                    <td>Vishal</td>
                    <td>Rajkot</td>
                    <td>23/10/2020</td>
                </tr>
                <tr>
                    <td>Nikhil</td>
                    <td>Rajkot</td>
                    <td>25/10/2020</td>
                </tr>
                <tr>
                    <td>Mehul</td>
                    <td>jamanagar</td>
                    <td>22/10/2020</td>
                </tr>
            </tbody>
        </table>
    </div>
</body>

</html>
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:
http://localhost:8080/

It will help you...

#Codeigniter 4