HTTP Response
Class PdfResponse cung cấp helper HTTP response bảo mật, tuân thủ chuẩn để gửi PDF tới trình duyệt. Nó thiết lập mọi header cần thiết tự động, bao gồm security header ngăn MIME-sniffing và cache document nhạy cảm.
use Yeeefang\TcpdfNext\Laravel\Http\PdfResponse;Hiển thị Inline
Render PDF trực tiếp trong trình xem tích hợp của trình duyệt với Content-Disposition: inline:
use Yeeefang\TcpdfNext\Laravel\Facades\Pdf;
use Yeeefang\TcpdfNext\Laravel\Http\PdfResponse;
public function preview(Invoice $invoice)
{
$pdf = Pdf::create()
->setTitle("Invoice #{$invoice->number}")
->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, "Invoice #{$invoice->number}");
return PdfResponse::inline($pdf, "invoice-{$invoice->number}.pdf");
}Bắt buộc Download
Kích hoạt hộp thoại lưu file của trình duyệt với Content-Disposition: attachment:
public function download(Invoice $invoice)
{
$pdf = Pdf::create()
->setTitle("Invoice #{$invoice->number}")
->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, "Invoice #{$invoice->number}");
return PdfResponse::download($pdf, "invoice-{$invoice->number}.pdf");
}Security Header
Cả inline() và download() đều tự động thiết lập các header sau:
| Header | Giá trị | Mục đích |
|---|---|---|
Content-Type | application/pdf | MIME type chính xác |
Content-Disposition | inline hoặc attachment | Chế độ hiển thị |
X-Content-Type-Options | nosniff | Ngăn tấn công MIME-sniffing |
Cache-Control | no-store, no-cache, must-revalidate | Ngăn cache PDF nhạy cảm |
Content-Length | <số byte> | Cho phép thanh tiến trình download |
Các giá trị mặc định này tuân theo khuyến nghị OWASP secure headers.
Streaming PDF lớn
Với document vượt quá bộ nhớ khả dụng, stream chunk trực tiếp tới output buffer:
public function downloadLargeReport()
{
$pdf = Pdf::create()->setTitle('Annual Report');
foreach ($sections as $section) {
$pdf->addPage()
->setFont('Helvetica', '', 11)
->multiCell(0, 6, $section->content);
}
return PdfResponse::stream($pdf, 'annual-report.pdf');
}PdfResponse::stream() trả về StreamedResponse với bộ nhớ sử dụng ổn định bất kể kích thước document.
Method Signature
public static function inline(PdfDocumentInterface $pdf, string $filename): Response;
public static function download(PdfDocumentInterface $pdf, string $filename): Response;
public static function stream(PdfDocumentInterface $pdf, string $filename): StreamedResponse;Response Macro
Package đăng ký hai response macro tiện lợi:
return response()->pdf($pdf, 'report.pdf'); // download
return response()->pdfInline($pdf, 'report.pdf'); // inlineCác macro này ủy quyền cho method PdfResponse, nên mọi security header đều được áp dụng.
Làm sạch tên file
PdfResponse làm sạch tên file để ngăn header injection. Ký tự ngoài [a-zA-Z0-9._-] bị loại bỏ và .pdf được bắt buộc:
// Input: "../../etc/passwd" -> Đã làm sạch: "etcpasswd.pdf"
return PdfResponse::download($pdf, $userInput);Bước tiếp theo
- Pdf Facade — Tạo document và test
- Queue Job — Chuyển PDF nặng sang background worker
- Cấu hình — Tùy chỉnh header và hành vi mặc định