我有一个用PHP编写的应用程序使用Yii框架并托管在Openshift上。我需要强制下载使用mpdf创建的pdf文件(使用此扩展名http://www.yiiframework.com/extension/pdf/)。
在我的本地服务器上,以下行足以强制下载我想要的名称:
$mPDF1->Output($file_name , 'D');
在Openshift上,不强制下载,文件名不正确。
这是来自mpdf的代码,我认为它用于创建响应的标题:
else if ($dest=='D') {
header('Content-Description: File Transfer');
if (headers_sent())
$this->Error('Some data has already been output to browser, can\'t send PDF file');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream', false);
header('Content-Type: application/download', false);
header('Content-Type: application/pdf', false);
header('Content-disposition: attachment; filename="'.$name.'"');
}
这些是我在本地服务器上获得的响应标头:
HTTP/1.1 200 OK
Date: Tue, 07 Jul 2015 07:15:34 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.9
Expires: Sat, 26 Jul 1997 05:00:00 GMT
Cache-Control: public, must-revalidate, max-age=0
Pragma: public
Content-Description: File Transfer
Content-Transfer-Encoding: binary
Last-Modified: Tue, 07 Jul 2015 07:15:34 GMT
Content-disposition: attachment; filename="invoice.pdf"
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/pdf
这些是我在Openshift上的响应标题:
HTTP/1.1 200 OK
Date: Tue, 07 Jul 2015 07:05:05 GMT
Server: Apache/2.2.15 (Red Hat)
Expires: Sat, 26 Jul 1997 05:00:00 GMT
Cache-Control: public, must-revalidate, max-age=0
Pragma: public
Content-Description: File Transfer
Content-Transfer-Encoding: binary
Last-Modified: Tue, 07 Jul 2015 07:05:07 GMT
Content-Type: application/pdf
Vary: Accept-Encoding
Content-Encoding: gzip
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Transfer-Encoding: chunked
所以在Openshift上它缺失了:
Content-disposition: attachment; filename="invoice.pdf"
并且文件未下载,但显示在浏览器中。
我该怎么做才能让它发挥作用?
答案 0 :(得分:0)
如果您删除Content-Type:application / pdf,我认为它会起作用。有些浏览器会自动打开这种文件。只留下application / octet-stream。
header('Content-Transfer-Encoding: binary');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="'.$name.'"');
答案 1 :(得分:0)
我在从库中生成PDF后使用它。到目前为止没问题。
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="generated.pdf"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file_to_save));
header('Accept-Ranges: bytes');
readfile($file_to_save);
其中$file_to_save
是您的pdf的绝对路径