Lalit Singh
December 28, 2024
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.
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.
<!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>
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
<?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
}
}
?>
Here is the link to download Vendor folder which i have used in this tutorial.
Click Here to download
You just think to start your business, and let us work about the rest to help you to give you space in this Digital era.
Do not want to miss any news, updates, notice or any offer on our products, then please subscribe to our mailing list.
Copyright 2024 by Sunyam Technologies. All rights reserved.