Symfony – forcing file download as attachment

The following forces the browser to download the file as an ‘attachment’, rather than inline (within the browser window);

(in this case, named ‘my-pdf-filename.pdf’)

$response = new Response($pdfData, 200, [
   'Content-Length' => strlen($pdfData),
   'Content-type' => 'application/pdf',
   'Content-Transfer-Encoding' => 'binary'
]);

$disposition = $response->headers->makeDisposition(
   ResponseHeaderBag::DISPOSITION_ATTACHMENT,
   'my-pdf-filename.pdf'
);
$response->headers->set('Content-Disposition', $disposition);

 

Leave a Reply