CodeIgniter Paket
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.
Installationation
bash
composer require yeeefang/tcpdf-next-codeigniterAnforderungen: CodeIgniter ^4.0, PHP ^8.5
Konfiguration
Publish the configuration file:
bash
php spark tcpdf-next:publishphp
// 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);Paket Contents
| Class | Beschreibung |
|---|---|
TcpdfNextService | Service registration and factory |
PdfLibrary | Library class for controller usage |
PdfResponse | HTTP response with security headers |
GeneratePdfJob | Queue job for background generation |