Skip to content

CodeIgniter Package

TCPDF-Next CodeIgniter
CodeIgniter · LGPL-3.0

The CodeIgniter package provides CodeIgniter 4 integration for TCPDF-Next — service registration, a Library class for controllers, queue-based async generation, and HTTP response helpers with OWASP security headers.

Installation

bash
composer require yeeefang/tcpdf-next-codeigniter

Requirements: CodeIgniter ^4.0, PHP ^8.5

Configuration

Publish the configuration file:

bash
php spark tcpdf-next:publish
php
// app/Config/TcpdfNext.php
namespace Config;

use CodeIgniter\Config\BaseConfig;

class TcpdfNext extends BaseConfig
{
    public string $fontsDirectory = WRITEPATH . 'fonts';
    public string $defaultPageSize = 'A4';
    public string $defaultOrientation = 'portrait';
    public bool $autoPageBreak = true;
    public float $marginBottom = 25.0;
}

Service Usage

Use the service in your controllers:

php
namespace App\Controllers;

use Yeeefang\TcpdfNext\CodeIgniter\Services\TcpdfNextService;

class InvoiceController extends BaseController
{
    public function download(int $id)
    {
        $invoice = model('InvoiceModel')->find($id);

        $pdf = service('tcpdfnext')->create()
            ->setTitle("Invoice #{$invoice->number}")
            ->addPage()
            ->setFont('Helvetica', '', 12)
            ->cell(0, 10, "Invoice #{$invoice->number}");

        return service('tcpdfnext')->response($pdf, "invoice-{$invoice->number}.pdf");
    }
}

Library Usage

Alternatively, use the Library class:

php
namespace App\Controllers;

use Yeeefang\TcpdfNext\CodeIgniter\Libraries\PdfLibrary;

class ReportController extends BaseController
{
    public function generate()
    {
        $pdfLib = new PdfLibrary();

        $pdf = $pdfLib->create()
            ->setTitle('Monthly Report')
            ->addPage()
            ->setFont('Helvetica', 'B', 16)
            ->cell(0, 10, 'Monthly Report', newLine: true);

        return $pdfLib->inline($pdf, 'report.pdf');
    }
}

HTTP Responses

The response helpers include OWASP-recommended security headers:

php
// Inline display (browser preview)
return service('tcpdfnext')->inline($pdf, 'report.pdf');

// Force download
return service('tcpdfnext')->download($pdf, 'report.pdf');

Queue Integration

Dispatch PDF generation to CodeIgniter's task scheduler:

php
use Yeeefang\TcpdfNext\CodeIgniter\Queue\GeneratePdfJob;

$job = new GeneratePdfJob([
    'template' => 'invoice',
    'data' => ['invoice_id' => $invoice->id],
    'output_path' => WRITEPATH . "pdfs/invoice-{$invoice->id}.pdf",
]);

service('queue')->push($job);

Package Contents

ClassDescription
TcpdfNextServiceService registration and factory
PdfLibraryLibrary class for controller usage
PdfResponseHTTP response with security headers
GeneratePdfJobQueue job for background generation

Released under the LGPL-3.0-or-later License.