Pdf Facade
Pdf Facade cung cấp static proxy tiện lợi tới binding PdfDocumentInterface được đăng ký bởi TcpdfServiceProvider. Mọi giá trị cấu hình từ config/tcpdf-next.php được áp dụng tự động.
php
use Yeeefang\TcpdfNext\Laravel\Facades\Pdf;Tạo Document
Pdf::create() resolve instance Document mới từ container với mọi giá trị mặc định từ config đã được áp dụng sẵn:
php
$pdf = Pdf::create();
$pdf->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, 'Hello from Laravel!')
->save(storage_path('app/hello.pdf'));Mỗi lần gọi create() trả về instance mới. Document không bao giờ được chia sẻ giữa các lần gọi.
Fluent API
Mọi method của Document đều có sẵn qua Facade và trả về static để chain:
php
$pdf = Pdf::create()
->setTitle('Quarterly Report')
->setAuthor('Finance Team')
->setMargins(left: 15, top: 20, right: 15)
->addPage()
->setFont('Helvetica', 'B', 18)
->cell(0, 12, 'Q1 2026 Report')
->ln()
->setFont('Helvetica', '', 11)
->multiCell(0, 6, $reportBody);Configuration Binding
Facade đọc giá trị mặc định từ config/tcpdf-next.php. Bạn có thể ghi đè bất kỳ cài đặt nào cho từng document:
php
// Dùng giá trị mặc định từ config (A4 portrait, Helvetica 11pt)
$default = Pdf::create();
// Ghi đè page format chỉ cho document này
$receipt = Pdf::create()
->setPageFormat('A5')
->setMargins(left: 10, top: 10, right: 10);Xem Cấu hình để tham chiếu đầy đủ.
Facade Class Reference
php
namespace Yeeefang\TcpdfNext\Laravel\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @method static \Yeeefang\TcpdfNext\Core\Document create()
* @method static void fake()
* @method static void assertCreated()
* @method static void assertCreatedCount(int $count)
*
* @see \Yeeefang\TcpdfNext\Core\Document
*/
class Pdf extends Facade
{
protected static function getFacadeAccessor(): string
{
return \Yeeefang\TcpdfNext\Contracts\PdfDocumentInterface::class;
}
}Test với Pdf::fake()
Thay binding thật bằng fake để assertion mà không tạo byte PDF thật:
php
use Yeeefang\TcpdfNext\Laravel\Facades\Pdf;
public function test_invoice_generates_pdf(): void
{
Pdf::fake();
$response = $this->get('/invoices/42/pdf');
$response->assertOk();
Pdf::assertCreated();
Pdf::assertCreatedCount(1);
}Fake ghi lại mọi lời gọi method, nên bạn có thể kiểm tra fluent chain:
php
Pdf::fake();
$this->get('/invoices/42/pdf');
Pdf::assertCreated(function ($pdf) {
return $pdf->getTitle() === 'Invoice #42'
&& $pdf->getPageCount() >= 1;
});Bước tiếp theo
- HTTP Response — Gửi PDF tới trình duyệt bảo mật
- Queue Job — Chuyển tạo PDF sang background worker
- Cấu hình — Mọi key config có sẵn