Paket Laravel
Laravel · LGPL-3.0Paket 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-laravelPersyaratan: Laravel ^12.0
ServiceProvider di-auto-discover. Publish konfigurasi:
bash
php artisan vendor:publish --tag=tcpdf-next-configQuick 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
| Interface | Binding | Scope |
|---|---|---|
PdfDocumentInterface | Document::create() | Factory (baru per resolusi) |
FontManagerInterface | FontManager | Scoped (segar per request, aman Octane) |
SignerInterface | Dikonfigurasi dari config/tcpdf-next.php | Factory |
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-queueDependency 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
- Facade Pdf -- Akses statis dan helper testing
- Respons HTTP -- Tampilan inline dan download aman
- Queue Job -- Pembuatan PDF async dengan callback
- Konfigurasi -- Referensi konfigurasi lengkap
