Codeigniter 4 Pdf Tutorial Generate pdf in Codeigniter

This Codeigniter 4 Pdf Tutorial guide will show you how to integrate the domPDF plugin into CodeIgniter, especially if you’re new to CodeIgniter development.

HTML to PDF conversion often confuses new developers. We aim to make this process straightforward for you.

At the conclusion of this tutorial, you’ll be able to generate a PDF file from an HTML design using the DomPDF library.

DomPDF is a tool designed for converting HTML into PDF files.

Step 1 – Set Up CodeIgniter 4

Visit abc.com to download the CodeIgniter 4 ZIP file. Extract the contents of this ZIP file into the xampp/htdocs directory.

Step 2 – Install TCPDF

Execute the following command in your terminal to add the TCPDF library to your project:

composer require tecnickcom/tcpdf

Step 3 – Create a PDF Service

Navigate to the app/Services folder and create a file named PdfService.php to manage PDF creation:

<?php

namespace App\Services;

use TCPDF;

class PdfService
{
protected $pdf;

public function __construct()
{
$this->pdf = new TCPDF();
}

public function generatePDF($title, $content)
{
// Configure document settings
$this->pdf->SetCreator(PDF_CREATOR);
$this->pdf->SetAuthor('Your Name');
$this->pdf->SetTitle($title);

// Set header information
$this->pdf->SetHeaderData('', 0, $title, '');

// Define header and footer fonts
$this->pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$this->pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// Specify default monospaced font
$this->pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// Configure margins
$this->pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$this->pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$this->pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// Enable automatic page breaks
$this->pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// Set the image scaling factor
$this->pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// Add a new page
$this->pdf->AddPage();

// Set the font
$this->pdf->SetFont('dejavusans', '', 12);

// Insert the content
$this->pdf->writeHTML($content, true, false, true, false, '');

// Return the PDF as a string
return $this->pdf->Output('document.pdf', 'I');
}
}

Step 4 – Create a Controller for PDF Generation

In app/Controllers, create a file named PdfController.php to handle PDF generation by calling the service:

<?php

namespace App\Controllers;

use App\Services\PdfService;
use CodeIgniter\Controller;

class PdfController extends Controller
{
protected $pdfService;

public function __construct()
{
$this->pdfService = new PdfService();
}

public function index()
{
$title = "Sample PDF Document";
$content = "<h1>Welcome to the CodeIgniter 4 PDF Tutorial</h1><p>This is an example of a PDF generated with the TCPDF library in CodeIgniter 4.</p>";

return $this->pdfService->generatePDF($title, $content);
}
}

Step 5 – Define a Route

Modify the app/Config/Routes.php file to include the following route:

$routes->get('/pdf', 'PdfController::index');

Step 6 – Test PDF Generation

Run the following command to start the application:

php spark serve

Enter the following URL in the browser to convert the HTML into PDF in Codeigniter application:

http://localhost:8080