Skip to content

Pacote Laravel

TCPDF-Next Laravel
Laravel · LGPL-3.0

O pacote Laravel (yeeefang/tcpdf-next-laravel) fornece integração de primeira classe com o Laravel 12:

  • ServiceProvider auto-discovered -- Bindings DI, serviços escopados seguros para Octane
  • Facade Pdf -- acesso estático conveniente
  • PdfResponse -- helpers de resposta HTTP seguras (inline/download)
  • GeneratePdfJob -- geração assíncrona de PDF via fila

Instalação

bash
composer require yeeefang/tcpdf-next-laravel

Requisitos: Laravel ^12.0

O ServiceProvider é auto-discovered. Publique a configuração:

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

Início Rápido

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 do Service Provider

InterfaceBindingEscopo
PdfDocumentInterfaceDocument::create()Factory (novo por resolução)
FontManagerInterfaceFontManagerEscopado (novo por request, seguro para Octane)
SignerInterfaceConfigurado a partir de config/tcpdf-next.phpFactory

O TcpdfServiceProvider mescla padrões sensatos do arquivo de configuração incluído e registra todos os bindings antes da aplicação inicializar. Em ambientes Laravel Octane, os bindings escopados são automaticamente limpos entre requests para prevenir vazamento de estado.

Estrutura do Pacote

Yeeefang\TcpdfNext\Laravel\
├── TcpdfServiceProvider       # DI bindings, config publishing
├── Facades\
│   └── Pdf                    # Static proxy to Document factory
├── Http\
│   └── PdfResponse            # Inline / download response helpers
└── Jobs\
    └── GeneratePdfJob         # Queueable async PDF generation

Injeção de Dependência

Em vez da Facade, injete o contrato diretamente:

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

Próximos Passos

Distribuído sob a licença LGPL-3.0-or-later.