Pdf Generation in Codeigniter using mpdf library
Pdf Generation has a big usage in software industry. In this tutorial, We will learn how to generate Pdf files in Codeigniter using mpdf library and Composer. Getting Pdf Invoices, Ebooks and other documents are used most frequently in Softwares and Websites. Hence, Generating your own pdfs from your own Software is what we will be learning in this tutorial. Lets get on to it:
We Will Follow the Following Steps:
- Install Codeigniter in your local server
- Install Composer
- Install mpdf library
- Configure mpdf into Codeigniter
- Create a Controller
- Create a View
- Preview in Browser
Install Codeigniter in your Local server
Download the latest version of Codeigniter from https://codeigniter.com/download
Extract the files and paste them in a folder in your local server[Xampp] and rename the folder to your liking. In this tutorial we are naming the folder as pdftut.
Install Composer
First of all, we will be needing Composer to install mpdf in the directory.
If you are a windows user, just follow the link https://getcomposer.org/download/.
Install the File and to confirm installation, go to the Command prompt and type:composer
For Mac and Linux users, Just follow the Link https://getcomposer.org/doc/00-intro.md
Install mpdf library
Go the the Terminal, and go to the project folder path.
Now, enter composer command to install mpdf library:
composer require mpdf/mpdf
This will take sometime and it will install all the required files in a folder called Vendor. After Successful installation, exit the terminal.
Configure mpdf into Codeigniter
We need to set the directory path for mpdf to be used in the program. Open the config.php in applications/config folder and the composer autoload:
$config['composer_autoload'] = 'vendor/autoload.php';
Create a Controller
Lets create a Controller and name it GenPDF.php. Secondly, create the index function and enter the following code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class GenPDF extends CI_Controller {
public function index()
{
$mpdf = new \Mpdf\Mpdf();
$html = $this->load->view('genpdf_view',[],true);
$mpdf->WriteHTML($html);
$mpdf->Output();
}
}
Create a View
Lets create a view and rename it genpdf_view. Secondly, enter the following html [ your own html code]:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My PDF</title>
</head>
<body>
<div class="container">
<h1>My Own Pdf</h1>
<div class="row">
<p>This is a demo text used to show how PDF is generated dynamically</p>
<p>Try your own in this block</p>
<p>This is a paragraph. Edit this to enter your own</p>
<p>We have successfully generated our first PDF.</p>
</div>
</div>
</body>
</html>
Preview in Browser
Now, you can type in the project url and view the page in Browser. Just enter the following url for this project [I am using Xampp Sever]:
localhost/pdftut/
Conclusion
We have successfully created our first pdf from html. This can be integrated to softwares and websites. We can generate Invoices, create Ebooks and much more.
For more support, Contact Us