Skip to content

Respons HTTP

Class PdfResponse menyediakan helper respons HTTP yang aman dan sesuai standar untuk mengirimkan PDF ke browser. Class ini mengatur semua header yang diperlukan secara otomatis, termasuk header keamanan yang mencegah MIME-sniffing dan caching dokumen sensitif.

php
use Yeeefang\TcpdfNext\Laravel\Http\PdfResponse;

Tampilan Inline

Render PDF langsung di built-in viewer browser dengan 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");
}

Paksa Download

Picu dialog simpan file browser dengan 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");
}

Header Keamanan

Baik inline() maupun download() secara otomatis mengatur header ini:

HeaderNilaiTujuan
Content-Typeapplication/pdfTipe MIME yang benar
Content-Dispositioninline atau attachmentMode tampilan
X-Content-Type-OptionsnosniffMencegah serangan MIME-sniffing
Cache-Controlno-store, no-cache, must-revalidateMencegah caching PDF sensitif
Content-Length<jumlah byte>Mengaktifkan progress bar download

Default ini mengikuti rekomendasi OWASP secure headers.

Streaming PDF Besar

Untuk dokumen yang melebihi memori yang tersedia, stream chunk langsung ke 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() mengembalikan StreamedResponse dengan penggunaan memori konstan terlepas dari ukuran dokumen.

Signature Method

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

Paket mendaftarkan dua response macro untuk kenyamanan:

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

Macro ini mendelegasikan ke method PdfResponse, sehingga semua header keamanan diterapkan.

Sanitisasi Nama File

PdfResponse mensanitasi nama file untuk mencegah header injection. Karakter di luar [a-zA-Z0-9._-] dihapus dan .pdf dipaksakan:

php
// Input: "../../etc/passwd"  ->  Disanitasi: "etcpasswd.pdf"
return PdfResponse::download($pdf, $userInput);

Langkah Selanjutnya

Didistribusikan di bawah lisensi LGPL-3.0-or-later.