Convert HTML to pdf using php codeigniter

Codeigniter

Hello Friends,

Whenever anyone wants to print any page, they have to press ctrl+P, but as a developer this is our responsibility to provide an option to convert an html code or page to pdf without pressing any special button.

We generally wanted this type of code to create invoices.

Full tutorial video is available on youtube, and given link of the video at end of this page.

View page source code

In this tutorial we write this code in views/front/print.php

Obviously you can change file name and code also according to your requirement.

Markup
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Convert HTML to PDF using php codeigniter</title>
</head>
<body>
    <center><h2>Sunyam Technologies</h2></center>
</body>
</html>

Controller source code

We have write below code in controllers/print_code.php

*important : while using page loader $this->load->view(‘front/print’, ”, true); always remember to give 3 parameters :

1 – page,

2 – either your code to send to the page or leave blank like ”

3 – true

Markup
<?php

require FCPATH.'vendor/autoload.php';
defined('BASEPATH') OR exit('No direct script access allowed');

class Print_docs extends CI_Controller {

	function __construct(){
		
		parent::__construct();
		$this->load->database();
	}

	public function index(){
		$html = $this->load->view('front/print', '', true);  // remember to use 3 parameter in the view 1 - page, 2 - should blank, or you should give your data to the page like this, but i will pass blank, and this is required , 3 - pass true

		$mpdf = new \Mpdf\Mpdf();
		$mpdf->WriteHTML($html); // load your html code inside WriteHTML
		$mpdf->Output();  // remember to use this code as it is, don't use small case in all 
	}
}
?>

Vendor folder link

Here is the link to download Vendor folder which i have used in this tutorial.

Click Here to download

Tutorials List