For any queries you can reach us at / WhatsApp us: +919158876092

Generate PDF in CodeIgniter 4


In this example, I am going to show you how to generate PDF using the CodeIgniter framework PHP.

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

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


composer require dompdf/dompdf

Here, we are using 3 files for generating PDF file:

Controller: 	Generate.php		app\Controllers\Generate.php
Library: 	Pdf.php		        app\Libraries\Pdf.php
View:		pdf_view.php            app\Views\pdf_view.php


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


namespace App\Libraries;

use Dompdf\Dompdf;

class Pdf extends Dompdf {
	public function __construct() {


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
		// set page size and orientation
		$this->pdf->setPaper('A4', 'portrait');
		// generate pdf
		// download or view pdf 
		$this->pdf->stream("file.pdf", array("Attachment"=> 1));	


<!DOCTYPE html>

<h1>My PDF Heading</h1>

<p>My PDF paragraph.</p>


How to run?

Open your browser and refer following link: