Codeigniter PDF Generator Tutorial Using Mpdf library

Codeigniter PDF Generator Tutorial Using Mpdf library

In this Codeigniter generate pdf tutorial, You will learn how to create pdf file using mpdf library with composer. We would love to share with you how to use CodeIgniter 3 and composer to install and to load mPDF library.

For some time we need to generate PDF for invoice, bill payment, data, information, membership package etc. You want to send any proposal to the users in the email, at that time you can send to PDF mail users.

In this tutorial we will use composer command to download the mpdf library in codeigniter setup.

Here is the few steps to create html to pdf file using mpdf library.

Codeigniter Create PDF

  • 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 also rename your project folder name to demo.

Install Mpdf Library

Now open your terminal Go to your project folder.

cd your project folder name

Run below composer command for install mPDF library from your project folder. This command install all the files of mpdf library inside the vendor folder.

composer require mpdf/mpdf 

Basic Configurations

Next we will set the some basic configuration on config.php file, so let’s go to application/config/config.php and open this file on text editor.

Set Base URL like this
$config['base_url'] = 'http://localhost/demo';
Set vendor directory path

Open application/config/config.php file and set you vendor directory path

$config['composer_autoload'] = 'vendor/autoload.php';

Create Controller

Now we need to create a controller name Mypdf.php. In this controller we will create method/function name index() .

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->Output(); // opens in browser
        //$mpdf->Output('test.pdf','D'); // it downloads the file into the user system.

Create View

Now we will create a view called html_convert_pdf.php file in side in your view folder with Using below html.

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <title>Welcome to CodeIgniter</title>
<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>
        <p>The corresponding controller for this page is found at:</p>
        <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>

Start Development server

For start development server, Go to the browser and hit below the url.




In this codeigniter generate or create pdf tutorial – We have created html to pdf using mpdf library and download this library using composer command.

You may like

  1. Export Data to Excel/CSV in Codeigniter Using PHPExcel
  2. Adding Multiple Markers To Google Maps From Database PHP Codeigniter
  3. Integrate Razorpay with PHP Codeigniter
  4. Implement Google Column Chart With PHP Codeigniter
  5. Google Bar & Line Charts Days Wise MySQL PHP Codeigniter
  6. Codeigniter Pagination Library Example
  7. Morris Area & Line Chart With Codeigniter Example

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Codeigniter, Vue JS, Angular JS, React Js, WordPress and Bootstrap from a starting stage.

Leave a Reply

Your email address will not be published. Required fields are marked *