Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cakephp tcpdf在safari和opera中生成.html文档_Cakephp_Cakephp 2.1_Tcpdf - Fatal编程技术网

Cakephp tcpdf在safari和opera中生成.html文档

Cakephp tcpdf在safari和opera中生成.html文档,cakephp,cakephp-2.1,tcpdf,Cakephp,Cakephp 2.1,Tcpdf,我将tcpdf与Cakephp结合使用。PDF在Linux中下载得很好,但在Mac for Opera和Safari中添加了.html文件 我还注意到,即使它在FireFox和Google Chrome中以PDF文档的形式下载,另存为的弹出窗口也会将其读作“HTML文档”,但另存为PDF。请帮我解决这个问题。尝试修改标题,如: header("Content-Description: File Transfer"); header('Content-Type: application/octet

我将tcpdf与Cakephp结合使用。PDF在Linux中下载得很好,但在Mac for Opera和Safari中添加了.html文件


我还注意到,即使它在FireFox和Google Chrome中以PDF文档的形式下载,另存为的弹出窗口也会将其读作“HTML文档”,但另存为PDF。请帮我解决这个问题。

尝试修改标题,如:

header("Content-Description: File Transfer");
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file_name));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Cache-Control: private", false); // required for certain browsers
header('Pragma: public');
header('Content-Length: ' . filesize($file_name));

希望它能起作用。:)

似乎Mac和Safari在mime类型方面更加严格,并且始终遵守脚本中设置的内容类型。因此,如果内容类型被设置为text/html,Safari将希望它是一个html文档,并添加html扩展。Firefox和Chrome使用此内容类型标题来显示文件类型(HTML文档),但不会更改文件名,因此它会保存为PDF

在使用CakePHP时,最好在控制器中使用内置的文件响应:

$this->response->file($path,array('download' => true, 'name' => $filename));
return $this->response;
这将正确设置大多数文件所需的标题。有关文档,请参见此处:

请注意,CakePHP可能不知道某些文件的mimetype,并将默认为text/html。那么在Safari中也会遇到同样的问题。最好进行测试,如果您看到这种情况,请自己设置mime类型(在设置文件之前):

我通过添加

$this->response->type('application/pdf');

通过更新到TCPDF供应商文件的最新版本,它为我们解决了相同的问题(以及其他显示问题)。

在您的控制器中,只需添加此项即可

$this->response->header(array('Content-type: application/pdf')); 
$this->response->type('pdf'); 

通过添加此项,我也解决了同样的问题。

PDF是从控制器输出还是在视图中输出?您使用的是什么版本的CakePHP?是的,输出在视图本身内部。Cakephp版本是2.1.5
$this->response->header(array('Content-type: application/pdf')); 
$this->response->type('pdf');