Codeigniter 4 PDF Tutorial

Codeigniter

Nicesnippets

13046

27-10-2020


Hi Guys,

In this example,I will learn you how to create pdf in codeigniter 4.you 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.

<?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();

}

}

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">

<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 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>City</th>

<th>Date</th>

</tr>

</thead>

<tbody>

<tr>

<td>Ravi</td>

<td>Rajkot</td>

<td>22/10/2020</td>

</tr>

<tr>

<td>Sunil</td>

<td>Jamanagar</td>

<td>23/10/2020</td>

</tr>

<tr>

<td>Jd</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>

It will help you...