Skip to content

Package Laravel

TCPDF-Next Laravel
Laravel · LGPL-3.0

Le package Laravel (yeeefang/tcpdf-next-laravel) fournit une intégration Laravel 12 de première classe avec :

  • ServiceProvider auto-découvert — bindings DI, services scopés sûrs pour Octane
  • Facade Pdf — accès statique pratique
  • PdfResponse — helpers de réponse HTTP sécurisés (inline/download)
  • GeneratePdfJob — génération PDF async basée sur file d'attente

Installation

bash
composer require yeeefang/tcpdf-next-laravel

Prérequis : Laravel ^12.0

Le ServiceProvider est auto-découvert. Publiez la config :

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

Démarrage rapide

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");
    }
}

Bindings du Service Provider

InterfaceBindingScope
PdfDocumentInterfaceDocument::create()Factory (nouveau par résolution)
FontManagerInterfaceFontManagerScoped (frais par requête, sûr pour Octane)
SignerInterfaceConfiguré depuis config/tcpdf-next.phpFactory

Le TcpdfServiceProvider fusionne des valeurs par défaut sensées depuis le fichier de config fourni et enregistre tous les bindings avant le démarrage de votre application. Dans les environnements Laravel Octane, les bindings scopés sont automatiquement vidés entre les requêtes pour éviter les fuites d'état.

Structure du package

Yeeefang\TcpdfNext\Laravel\
├── TcpdfServiceProvider       # Bindings DI, publication config
├── Facades\
│   └── Pdf                    # Proxy statique vers factory Document
├── Http\
│   └── PdfResponse            # Helpers réponse inline / download
└── Jobs\
    └── GeneratePdfJob         # Génération PDF async queueable

Injection de dépendance

Au lieu de la Facade, injectez directement le contrat :

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();
    }
}

Prochaines étapes

Distribué sous licence LGPL-3.0-or-later.