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() .

<?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

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">
<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>

Start Development server

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

http://localhost/demo/index.php/mypdf/index

or
 
http://localhost/demo/mypdf/index
 

Conclusion

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

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

Spread the love

Be First to Comment

Leave a Reply

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