0

我将 tcpdf 与 Cakephp 一起使用。PDF 在 Linux 中可以很好地下载,但对于 Opera 和 Safari 的 Mac,它会添加 .html 扩展名。

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

4

4 回答 4

1

尝试修改标题,例如:

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

希望它有效。:)

于 2013-05-27T10:20:25.360 回答
0

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

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

$this->response->file($path,array('download' => true, 'name' => $filename));
return $this->response;

这将正确设置大多数文件所需的标题。有关文档,请参见此处:http: //book.cakephp.org/2.0/en/controllers/request-response.html#sending-files

请注意,CakePHP 可能不知道某些文件的 mimetype,并且默认为 text/html。然后你会在 Safari 中遇到同样的问题。测试是个好主意,如果您看到这种情况发生,请自己设置 mime 类型(在设置文件之前):

$ext = pathinfo($filename, PATHINFO_EXTENSION);
switch ($ext) {
    case 'dotx':
        $this->response->type('application/vnd.openxmlformats-officedocument.wordprocessingml.template');
    break;
}
$this->response->file($path,array('download' => true, 'name' => $filename));
return $this->response;
于 2015-03-13T14:50:26.877 回答
0

我通过添加发现

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

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

于 2015-03-16T16:50:29.223 回答
0

在您的控制器中只需添加这个

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

通过添加同样的问题来解决我。

于 2015-08-10T08:32:42.550 回答