HTTP response headers in Zend Framework

I needed to display a Zend Framework generated PDF using the browser plugins and after a bit of looking around it turns out that some headers need to be set in order to achieve that.

$this->getResponse()
     ->setHeader('Content-Disposition', 'inline; filename=invoice.pdf')
     ->setHeader('Content-type', 'application/x-pdf');

If you’d want to automatically download the PDF instead of displaying it using the browser plugins, you need to change the ‘Content-Disposition’ as follows:

$this->getResponse()
     ->setHeader('Content-Disposition', 'attachment; filename=invoice.pdf')
     ->setHeader('Content-type', 'application/x-pdf');

1 thought on “HTTP response headers in Zend Framework”

  1. You’ll probably want to set the 3rd argument to setHeader to true:
    ->setHeader(‘Content-Disposition’, ‘attachment; filename=invoice.pdf’, true)

    This will avoid the duplicate headers error which new versions of Chrome will throw.

Leave a Reply

Your email address will not be published. Required fields are marked *