The mPDF library allow users to generate PDFs for invoices, bill payments, data, information, subscription packages, etc.
In this tutorial guide, You will learn how to generate pdf files using mpdf library.
How to Generate PDF in Codeigniter using mpdf
Here are steps:
- Download Codeigniter Latest
- Install Mpdf Library
- Basic Configurations
- Create Controller
- Create View
- Start Development server
- Conclusion
Download Codeigniter Project
In this step, we will download the latest version of Codeigniter, Go to this link Download Codeigniter. Download the fresh setup of Codeigniter and unzip the setup in your local system xampp/htdocs/
and rename your project folder name to demo.
Install Mpdf Library
Run the following composer command for installing the mPDF library from your project folder. This command installs all the files of the mpdf library inside the vendor folder.
composer require mpdf/mpdf
Basic Configurations
Set project base URL in application/config/config.php file:
$config['base_url'] = 'http://localhost/demo';
Open application/config/config.php
file and set you vendor directory path:
$config['composer_autoload'] = 'vendor/autoload.php';
Create Controller
Create Mypdf.php controller file and methods in it to handle PDF generation using mPDF:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Mypdf extends CI_Controller {
public function index()
{
$mpdf = new \Mpdf\Mpdf();
$html = $this->load->view('html_convert_pdf',[],true);
$mpdf->WriteHTML($html);
$mpdf->Output(); // opens in browser
//$mpdf->Output('test.pdf','D'); // it downloads the file into the user system.
}
}
Create View
Create html_convert_pdf.php file in applications/views folder to allow users to generate pdf from views:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
</head>
<body>
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
application/views/welcome_message.php
<p>The corresponding controller for this page is found at:</p>
application/controllers/Welcome.php
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>
</div>
</div>
</body>
</html>
Test This Project
Go to the browser and hit below the url.
http://localhost/demo/index.php/mypdf/index
Conclusion
That’s it, We have created html to pdf using mpdf library and download this library using composer command.
You may like
- Export Data to Excel/CSV in Codeigniter Using PHPExcel
- Adding Multiple Markers To Google Maps From Database PHP Codeigniter
- Integrate Razorpay with PHP Codeigniter
- Implement Google Column Chart With PHP Codeigniter
- Google Bar & Line Charts Days Wise MySQL PHP Codeigniter
- Codeigniter Pagination Library Example
- Morris Area & Line Chart With Codeigniter Example
If you have any questions or thoughts to share, use the comment form below to reach us.