Course Content
Introduction to CodeIgniter 4
CodeIgniter is an Application Development Framework. CodeIgniter is a popular and powerful MVC (Model-View-Controller) framework that is used to develop web applications. It is a free and Open-source PHP framework.
0/5
MVC (Model-View-Controller)
MVC stands for Model-View-Controller. MVC is an application design model consisting of three interconnected parts. They include the model (data), the view (user interface), and the controller (processes that handle input).
0/6
Sessions
The Session class allows you to maintain a user’s "state" and track their activity while they browse your site.
0/1
URI Routing
There is a one-to-one relationship between a URL string and its corresponding controller class/method.
0/2
Working with Database
Like any other framework, we need to interact with the database very often and CodeIgniter makes this job easy for us. It provides a rich set of functionalities to interact with the database.
0/5
Spreadsheet
PhpSpreadsheet is a PHP library for reading and writing spreadsheet files. Importing Excel and CSV into MySQL help to save the user time and avoid repetitive work.
0/1
CodeIgniter 4
About Lesson

For generating PDF files in CodeIgniter, we use an external library dompdf. By using Composer you can download this library into your project.

You need to run the following command in your project’s root directory.

Example:

composer require dompdf/dompdf

1. Copy and Paste the following lines in app/Config/Routes.php

$routes->add('Generate/generate_pdf', 'Generate::generate_pdf');

2. Create a controller file Pdf.php in the app/Libraries/ directory.

<?php 
namespace App\Libraries;

use Dompdf\Dompdf;

class Pdf extends Dompdf {
	
	public function __construct() {
		parent::__construct();
	}
}

3. Create a controller file Generate.php in the app/Controllers/ directory.

<?php 
namespace App\Controllers;

use App\Controllers\BaseController;
use App\Libraries\Pdf;
use Dompdf\Dompdf;

class Generate extends BaseController {

	public function __construct() {
	
		$this->parser = service('renderer');
		
	}
	
	public function generate_pdf() {
	
	    $dompdf = new Dompdf();
	    // Get Ouput from View
	    $html = $this->parser->render('pdf_view');
		// Load $html in dompdf
		$this->pdf->loadHtml($html);
		// set page size and orientation
		$this->pdf->setPaper('A4', 'portrait');
		// generate pdf
		$this->pdf->render();
		// download or view pdf 
		$this->pdf->stream("file.pdf", array("Attachment"=> 1));	
	}
}

4. Create a pdf_view.php file in the app/Views/ directory.

<!DOCTYPE html>
<html>
<body>

<h1>My PDF Heading</h1>

<p>My PDF paragraph.</p>

</body>
</html>