Skip to content

Laravelパッケージ

Laravel · LGPL-3.0

Laravelパッケージ(yeeefang/tcpdf-nextlaravel)は、以下を含むファーストクラスのLaravel 12統合を提供します:

  • 自動検出ServiceProvider — DIバインディング、Octane対応スコープサービス
  • Pdf ファサード — 便利な静的アクセス
  • PdfResponse — セキュアなHTTPレスポンスヘルパー(インライン/ダウンロード)
  • GeneratePdfJob — キューベースの非同期PDF生成

インストール

bash
composer require yeeefang/tcpdf-nextlaravel

要件: Laravel ^12.0

ServiceProviderは自動検出されます。設定ファイルをパブリッシュするには:

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

クイックスタート

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

Service Providerバインディング

インターフェースバインディングスコープ
PdfDocumentInterfaceDocument::create()ファクトリ(解決ごとに新規)
FontManagerInterfaceFontManagerスコープ(リクエストごとに更新、Octane対応)
SignerInterfaceconfig/tcpdf-next.php から設定ファクトリ

TcpdfServiceProvider は同梱の設定ファイルから適切なデフォルト値をマージし、アプリケーションがブートする前にすべてのバインディングを登録します。Laravel Octane環境では、スコープバインディングはリクエスト間で自動的にフラッシュされ、状態のリークを防止します。

パッケージ構造

Yeeefang\TcpdfNext\Laravel\
├── TcpdfServiceProvider       # DIバインディング、設定パブリッシュ
├── Facades\
│   └── Pdf                    # Documentファクトリへの静的プロキシ
├── Http\
│   └── PdfResponse            # インライン / ダウンロードレスポンスヘルパー
└── Jobs\
    └── GeneratePdfJob         # キュー可能な非同期PDF生成

依存性注入

ファサードの代わりに、コントラクトを直接注入できます:

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

次のステップ

LGPL-3.0-or-later ライセンスの下で公開されています。