Laravelパッケージ
Laravel · LGPL-3.0Laravelパッケージ(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バインディング
| インターフェース | バインディング | スコープ |
|---|---|---|
PdfDocumentInterface | Document::create() | ファクトリ(解決ごとに新規) |
FontManagerInterface | FontManager | スコープ(リクエストごとに更新、Octane対応) |
SignerInterface | config/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();
}
}