Skip to content

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.

php
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:

php
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:

php
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()download() đều tự động thiết lập các header sau:

HeaderGiá trịMục đích
Content-Typeapplication/pdfMIME type chính xác
Content-Dispositioninline hoặc attachmentChế độ hiển thị
X-Content-Type-OptionsnosniffNgăn tấn công MIME-sniffing
Cache-Controlno-store, no-cache, must-revalidateNgă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:

php
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

php
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:

php
return response()->pdf($pdf, 'report.pdf');         // download
return response()->pdfInline($pdf, 'report.pdf');   // inline

Cá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:

php
// 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

Phân phối theo giấy phép LGPL-3.0-or-later.