Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP ignores Content-Type setting on response

In my controller action, I have this:

$pdf = $this->Invoice->makePdf($invoice);
$this->response->charset('UTF-8');
$this->response->body($pdf);
$this->response->type(array('pdf' => 'application/x-pdf'));
$this->response->disableCache();
$this->response->send();

However, no matter what I do, CakePHP always sends the data as text/html; charset=utf-8. I have also tried

$this->response->header(array('Content-Type' => 'application/x-pdf'));

But it still sent it as text/html. How can I force the response to be sent using the content type above?

like image 555
Lanbo Avatar asked Feb 16 '23 23:02

Lanbo


1 Answers

Doing $this->response->type(array('pdf' => 'application/x-pdf')); stores/replaces the content type for the associated key as mentioned in the api. Use $this->response->type('pdf'); to set the type.

Edit: Also don't call $this->response->send(); just return the response object return $this->response; and let the dispatcher handle the sending.

like image 183
ADmad Avatar answered Feb 23 '23 13:02

ADmad