Skip to content

Pacchetto Laravel

TCPDF-Next Laravel
Laravel · LGPL-3.0

Il pacchetto Laravel (yeeefang/tcpdf-next-laravel) fornisce integrazione Laravel 12 di prima classe con:

  • ServiceProvider Auto-discovered — Binding DI, servizi scoped safe-Octane
  • Pdf Facade — accesso statico conveniente
  • PdfResponse — helper risposta HTTP sicuri (inline/download)
  • GeneratePdfJob — generazione PDF asincrona basata su queue

Installazione

bash
composer require yeeefang/tcpdf-next-laravel

Requisiti: Laravel ^12.0

Il ServiceProvider è auto-discovered. Pubblica la configurazione:

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

Avvio Rapido

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("Fattura #{$invoice->number}")
            ->addPage()
            ->setFont('DejaVuSans', '', 12)
            ->cell(0, 10, "Fattura #{$invoice->number}");

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

Binding Service Provider

InterfacciaBindingScope
PdfDocumentInterfaceDocument::create()Factory (nuovo per risoluzione)
FontManagerInterfaceFontManagerScoped (fresco per richiesta, Octane-safe)
SignerInterfaceConfigurato da config/tcpdf-next.phpFactory

Il TcpdfServiceProvider unisce default sensibili dal file config incluso e registra tutti i binding prima del boot della tua applicazione. In ambienti Laravel Octane, i binding scoped sono automaticamente svuotati tra richieste per prevenire perdita stato.

Struttura Pacchetto

Yeeefang\TcpdfNext\Laravel\
├── TcpdfServiceProvider       # Binding DI, pubblicazione config
├── Facades\
│   └── Pdf                    # Proxy statico a Document factory
├── Http\
│   └── PdfResponse            # Helper risposta inline / download
└── Jobs\
    └── GeneratePdfJob         # Generazione PDF asincrona queueable

Dependency Injection

Invece del Facade, inietta il contratto direttamente:

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, 'Rapporto Mensile')
            ->setFont('Helvetica', '', 12)
            ->cell(0, 10, "Generato: " . now()->format('F j, Y'))
            ->output();
    }
}

Prossimi Passi

Rilasciato sotto licenza LGPL-3.0-or-later.