Skip to content

Paket Laravel

TCPDF-Next Laravel
Laravel · LGPL-3.0

Paket Laravel (yeeefang/tcpdf-next-laravel) menyediakan integrasi Laravel 12 kelas satu dengan:

  • ServiceProvider yang auto-discover — DI binding, scoped service yang aman untuk Octane
  • Facade Pdf — akses statis yang nyaman
  • PdfResponse — helper respons HTTP yang aman (inline/download)
  • GeneratePdfJob — pembuatan PDF async berbasis queue

Instalasi

bash
composer require yeeefang/tcpdf-next-laravel

Persyaratan: Laravel ^12.0

ServiceProvider di-auto-discover. Publish konfigurasi:

bash
php artisan vendor:publish --tag=tcpdf-next-config

Quick Start

php
use Yeeefang\TcpdfNext\Laravel\Facades\Pdf;
use Yeeefang\TcpdfNext\Laravel\Http\PdfResponse;

class InvoiceController extends Controller
{
    public function download(Invoice $invoice)
    {
        $pdf = Pdf::create()
            ->setTitle("Invoice #{$invoice->number}")
            ->addPage()
            ->setFont('DejaVuSans', '', 12)
            ->cell(0, 10, "Invoice #{$invoice->number}");

        return PdfResponse::download($pdf, "invoice-{$invoice->number}.pdf");
    }
}

Binding Service Provider

InterfaceBindingScope
PdfDocumentInterfaceDocument::create()Factory (baru per resolusi)
FontManagerInterfaceFontManagerScoped (segar per request, aman Octane)
SignerInterfaceDikonfigurasi dari config/tcpdf-next.phpFactory

TcpdfServiceProvider menggabungkan default yang masuk akal dari file config bawaan dan mendaftarkan semua binding sebelum aplikasi Anda boot. Di lingkungan Laravel Octane, scoped binding otomatis di-flush antar request untuk mencegah kebocoran state.

Struktur Paket

Yeeefang\TcpdfNext\Laravel\
├── TcpdfServiceProvider       # DI binding, publishing config
├── Facades\
│   └── Pdf                    # Proxy statis ke factory Document
├── Http\
│   └── PdfResponse            # Helper respons inline / download
└── Jobs\
    └── GeneratePdfJob         # Pembuatan PDF async yang bisa di-queue

Dependency Injection

Alih-alih Facade, inject kontrak secara langsung:

php
use Yeeefang\TcpdfNext\Contracts\PdfDocumentInterface;

class ReportService
{
    public function __construct(
        private readonly PdfDocumentInterface $pdf,
    ) {}

    public function generate(array $data): string
    {
        return $this->pdf
            ->addPage()
            ->setFont('Helvetica', 'B', 16)
            ->cell(0, 10, 'Monthly Report')
            ->setFont('Helvetica', '', 12)
            ->cell(0, 10, "Generated: " . now()->format('F j, Y'))
            ->output();
    }
}

Langkah Selanjutnya

Didistribusikan di bawah lisensi LGPL-3.0-or-later.