Symfony: setHttpHeader () does not work, header () does

I created a simple action in symfony that generates a PDF file through wkhtmltopdf and displays it in a browser.

Here is the code:

$response = $this->getResponse(); $response->setContentType('application/pdf'); $response->setHttpHeader('Content-Disposition', "attachment; filename=filename.pdf"); $response->setHttpHeader('Content-Length', filesize($file)); $response->sendHttpHeaders(); $response->setContent(file_get_contents($file)); return sfView::NONE; 

This works fine in my local development environment - my browser receives the headers as expected, showing a download dialog.

Now I updated my test environment by running Apache 2.2.9-10 + lenny9 with PHP 5.3.5-0.dotdeb.0. If I now call this URL for the test environment, my browser will not receive custom set headers:

 Date Mon, 07 Mar 2011 10:34:37 GMT Server Apache Keep-Alive timeout=15, max=100 Connection Keep-Alive Transfer-Encoding chunked 

If I manually set them through header () in my action, Firebug will display the headers as expected. Does anyone know what could be wrong? Is it a symfony error or a problem with php or apache2 configuration? I do not understand.: -/

Thanks in advance!

+6
php apache2 symfony1
source share
3 answers

Your problem is here:

 return sfView::NONE; 

Change this to:

 return sfView::HEADERS_ONLY; 

Change the update due to additional comments.

Since you are trying to download a pdf file, you are approaching the problem incorrectly. Do not use sendContent() . See below (this is a snippet from the working site that I wrote and proved that it works in all major browsers):

 $file = '/path/to/file.pdf'; $this->getResponse()->clearHttpHeaders(); $this->getResponse()->setStatusCode(200); $this->getResponse()->setContentType('application/pdf'); $this->getResponse()->setHttpHeader('Pragma', 'public'); //optional cache header $this->getResponse()->setHttpHeader('Expires', 0); //optional cache header $this->getResponse()->setHttpHeader('Content-Disposition', "attachment; filename=myfile.pdf"); $this->getResponse()->setHttpHeader('Content-Transfer-Encoding', 'binary'); $this->getResponse()->setHttpHeader('Content-Length', filesize($file)); return $this->renderText(file_get_contents($file)); 
+10
source

The only difference I have is:

 $response = $this->getContext()->getResponse(); $response->clearHttpHeaders(); 
0
source

Received the same issue. Just add double quotes (") around the file name

 $this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="'.$filename.'"'); 

or

 header('Content-Disposition: attachment; filename="'.$filename.'"'); 
0
source

All Articles