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

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

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.

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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