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.
use Yeeefang\TcpdfNext\Laravel\Http\PdfResponse;Tampilan Inline
Render PDF langsung di built-in viewer browser dengan 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");
}Paksa Download
Picu dialog simpan file browser dengan 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");
}Header Keamanan
Baik inline() maupun download() secara otomatis mengatur header ini:
| Header | Nilai | Tujuan |
|---|---|---|
Content-Type | application/pdf | Tipe MIME yang benar |
Content-Disposition | inline atau attachment | Mode tampilan |
X-Content-Type-Options | nosniff | Mencegah serangan MIME-sniffing |
Cache-Control | no-store, no-cache, must-revalidate | Mencegah 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:
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
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:
return response()->pdf($pdf, 'report.pdf'); // download
return response()->pdfInline($pdf, 'report.pdf'); // inlineMacro 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:
// Input: "../../etc/passwd" -> Disanitasi: "etcpasswd.pdf"
return PdfResponse::download($pdf, $userInput);Langkah Selanjutnya
- Facade Pdf -- Pembuatan dokumen dan testing
- Queue Job -- Offload PDF berat ke background worker
- Konfigurasi -- Kustomisasi default header dan perilaku