Code your dreams into reality.
Every line of code is a step towards a better future.
Embrace the bugs, they make you a better debugger.

CodeIgniter 4 Generate a PDF Tutorial

Last Updated on July 31, 2024 by

The PHP TCPDF library provides developers with functions to make PDF creation easier.

Let’s start to generate pdf file:

Step 1 – Set Up CodeIgniter 4

Go to abc.com and download codeIgniter 4 zip file, and extract downloaded zip on xampp/htdocs directory.

Step 2 – Install TCPDF

Run the following command to install tcpdf library in project:

composer require tecnickcom/tcpdf

Step 3 – Create a PDF Service

Go to app/Services directory and create a file named PdfService.php that handle pdf generation:

<?php

namespace App\Services;

use TCPDF;

class PdfService
{
    protected $pdf;

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

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

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

        // Set 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));

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

        // Set 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);

        // Set auto page breaks
        $this->pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

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

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

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

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

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

Step 4 – Create a Controller for PDF Generation

Create a app/Controllers/PdfController.php file to call services in it to generate pdf file:

<?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 CodeIgniter 4 PDF Tutorial</h1><p>This is a sample PDF document generated using TCPDF library in CodeIgniter 4.</p>";

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

Step 5 – Define a Route

Edit app/Config/Routes.php file and define routes:

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

Step 6 – Test PDF Generation

Open browser and access http://your-domain.com/pdf url in it to generate pdf file.

Leave a Comment