Generate PDF with mPDF Library in CodeIgniter 4
In this blog post, we will explore how to generate PDF files using the mPDF library in a CodeIgniter 4 application with PHP 8.2. mPDF is a popular PHP library that allows you to create PDF documents easily from HTML content. This guide will walk you through the installation and implementation process step by step.
Prerequisites
Before we begin, ensure you have the following:
A working CodeIgniter 4 application.
PHP 8.2 installed on your server.
Basic knowledge of PHP and CodeIgniter.
Step 1: Install mPDF Library
To use the mPDF library, you need to install it via Composer. If you haven’t set up Composer in your CodeIgniter project, you can do so by following these steps:
Open your terminal or command prompt.
Navigate to your CodeIgniter project directory.
Run the following command to install mPDF:
composer require mpdf/mpdf
This command will download the mPDF library and add it to your vendor directory.
Step 2: Create a PDF Controller
Next, we will create a controller that will handle the PDF generation.
In your app/Controllers directory, create a new file named PdfController.php.
Open PdfController.php and add the following code:
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use Mpdf\Mpdf;
class PdfController extends Controller
{
public function generate()
{
// Create an instance of the mPDF class
$mpdf = new Mpdf();
// Define your HTML content
$html = '
<h1>Hello, World!</h1>
<p>This is a sample PDF generated using mPDF in CodeIgniter 4.</p>
';
// Write the HTML content to the PDF
$mpdf->WriteHTML($html);
// Output the PDF as a file
$mpdf->Output('sample.pdf', 'D'); // 'D' for download, 'I' for inline view
}
}
?>
In this code, we create a new instance of the mPDF class, define some HTML content, and then write that content to the PDF. Finally, we output the PDF file.
Step 3: Set Up a Route
To access the PDF generation functionality, we need to set up a route in CodeIgniter.
Open the app/Config/Routes.php file.
Add the following route:
$routes->get('generate-pdf', 'PdfController::generate');
This route will map the URL yourdomain.com/generate-pdf to the generate method in the PdfController.
Step 4: Test the PDF Generation
Now that everything is set up, you can test the PDF generation.
- 1. Start your CodeIgniter server if itβs not already running:
php spark serve
- 2. Open your web browser and navigate to http://localhost:8080/generate-pdf.
If everything is set up correctly, your browser should prompt you to download a file named sample.pdf, which contains the HTML content you defined in the controller.
In this tutorial, we have successfully integrated the mPDF library into a CodeIgniter 4 application to generate PDF files. You can customize the HTML content and styles as needed to create more complex PDF documents. mPDF offers a wide range of features, including support for CSS styles, images, and more, allowing you to create professional-looking PDFs with ease.
Feel free to explore the mPDF documentation for more advanced features and customization options. Happy coding!