views:

4266

answers:

4

Hi guys, I have a question, how can I do something like this:

header("Content-Disposition: inline; filename=result.pdf"); 
header("Content-type: application/x-pdf");

With Zend Framework, I have tried:

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

But doesn't work correctly.

Best Regards,

A: 

Solved

     $this->getResponse()
      ->setHeader('Content-Disposition:inline', ';filename=result.pdf')
    ->setHeader('Content-Type', 'application/x-pdf');
Uffo
Look at S. Gehrig's answer, it's the correct one. Yours may work, but only incidentally. ":inline" is not a part of the name of the header.
Rytmis
+16  A: 

Your statement to set the response headers is slightly malformed:

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

The above should work - please note the difference in the Content-Disposition-header.

By the way... When you want to force a download box (instead of loading the document in the browser) you should use the Content-Disposition attachment.

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

Depending on the browser it may be possible that you also have to set the Content-Length or change the Content-type to a combination (multiple headers) of one or more of application/force-download, application/octet-stream and/or application/download. And as I wrote in the comment sometimes caching headers may interfere with your download. Check to see which caching-headers are sent.

Stefan Gehrig
You're example doesn't work SR, i don't receive the download box anymore.Here is my code: http://pastebin.com/d6be142ab
Uffo
Which browser? And can you check what headers are actually sent over the wire? Especially some caching-headers may interfer with file-downloads.
Stefan Gehrig
@Uffo: If you want to force a download, you should say so in your question. You only asked what the correct way to set the headers is, and S. Gehrig's original reply was just that.
Rytmis
+3  A: 

Late to the table, I can recommend this action helper as a simple, reusable component for sending files or in memory data to the browser.

Has options for caching, disposition and can utilise Apache Sendfile

David Caunt
nice find thank you
Mark
Link is dead; any alternatives?
Aron Rotteveel
@Aron see http://github.com/noginn/noginn/blob/master/Noginn/Controller/Action/Helper/SendFile.php
David Caunt
Nice one, but this should not read whole file at once, but part-by-part. For big files it is an overkill.
takeshin
It can utilise Apache Sendfile which is much more efficient, if available
David Caunt
+1  A: 

My guess is that you're doing something like:

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

or something.

The response here will never be rendered (which renders the headers). The response is rendered during post-action printing, usually.

You will have to directly set the headers (as you noted in the non-oo code), or use $this->getResponse()->sendHeaders() directly.

Justin