Codeigniter Generate PDF Using Mpdf Tutorial

Codeigniter Generate PDF Using Mpdf Tutorial

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

  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 *