In this post, we will show you how to generate PDF in Codeigniter 4. As we know PDF is used to read any information in the form of a document. And a PDF document always reveals information for other documents such as applications, user manuals, e-books. The PDF file is always used via the .pdf file extension and the portable document format.
If you are also a Codeigniter developer and you want to create a PDF file from the HTML view template using the domPDF library in Codeigniter 4. Then this post will prove to be beneficial for you.
Install Codeigniter Application:
At this point, we are now going to install the CodeIgniter 4 application using the composer package.
composer create-project codeigniter4/appstarter
Here we have changed to Codeigniter-PDF-Example, now you rename the Codeigniter folder.
Then go inside the project folder.
cd codeigniter-pdf-example
Read Also: Codeigniter 4 Create MVC Example
Activate Errors Reporting:
At this point, we have to turn on the failure of the Codeigniter to deal with errors, which are turned off by default.
Now you go to app/Config/Boot/development.php and set the display_errors
to 1 instead of 0. Then do the exact same thing in app/Config/Boot/production.php.
ini_set('display_errors', '1');
Install & Configure DomPDF:
At this point, Now we are going to install the DomPDF plugin using the Composer package. Then we will run the below command to install the Composer plugin.
composer require dompdf/dompdf
Now we are installing the PDF package in Codeigniter application. Then, go to the app/Config/Autoload.php file and search for $psr4
array, then you have to register the domPDF service.
public $psr4 = [
APP_NAMESPACE => APPPATH, // For custom app namespace
'Config' => APPPATH . 'Config',
'Dompdf' => APPPATH . 'ThirdParty/dompdf/src',
];
Read Also: Example Of CodeIgniter 4 Form Validation
Create PDF Controller:
At this point, Now create two functions one for showing the tables in the view. then the second method helps to convert HTML into PDF. Generate a PdfController.php
file in the app/Controllers
folder. And 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:
At this point, you have to create a route that renders the table into the view, Then place the following code in the app/Config/Routes.php file.
$routes->get('/', 'PdfController::index');
Create View:
In this last point, you have to create a pdf_view.php file that you will use to convert HTML to PDF. Then 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 - discoverprograming.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>
Read Also: Codeigniter 4 Remove Public From URL
Start Application:
Now you will run the following command to start the application.
php spark serve
Enter the following URL in your browser to convert the HTML into PDF in Codeigniter application.
http://localhost:8080
Read Also: Download Codeigniter 4 By Manual,Composer,Git
Download the complete code of this tutorial fromĀ GitHub.
I hope it will help you…