4

在我的控制器操作中,我有这个:

$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();

但是,无论我做什么,CakePHP 总是将数据发送为text/html; charset=utf-8. 我也试过

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

但它仍然以text/html. 如何强制使用上述内容类型发送响应?

4

1 回答 1

8

执行$this->response->type(array('pdf' => 'application/x-pdf'));存储/替换api中提到的关联键的内容类型。用于$this->response->type('pdf');设置类型。

编辑:也不要调用$this->response->send();只返回响应对象return $this->response;并让调度程序处理发送。

于 2013-03-31T14:22:10.633 回答