我试图在php应用程序中显示.pdf文件。它在我的本地开发环境中很好地工作,但是在生产中,相同的代码将pdf显示为乱码文本。pdf文件显示为文本/ html而不是应用程序/ pdf
这些是请求头我正在使用:
<?php
$file = $_GET["f"];
$filename = 'contrato.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' .$filename. '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
readfile($file);
?>
这些都是根据铬生产响应头:
Connection:Keep-Alive
Content-Encoding:gzip
Content-Type:text/html
Date:Mon, 03 Apr 2017 21:31:19 GMT
Keep-Alive:timeout=15, max=189
Server:Apache
Transfer-Encoding:chunked
Vary:Accept-Encoding
在开发设置,响应头是这些:
Accept-Ranges:bytes
Connection:Keep-Alive
Content-Disposition:inline; filename="contrato.pdf"
Content-Transfer-Encoding:binary
Content-Type:application/pdf
Date:Mon, 03 Apr 2017 21:33:44 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.7 (Win32) PHP/5.5.8
Transfer-Encoding:chunked
X-Powered-By:PHP/5.5.8
难道这是由于Apache的设置?
只是为了检查:'%PDF-1.3':乱码输出的东西,如开始?您需要打开PHP中的错误报告。在发送标题之前查看是否有任何内容已经发送。 –
可能有些代理或服务器正在改变它,或者您的请求改变标头被PHP拒绝。检查你的错误:http://stackoverflow.com/q/845021/1255289 – miken32
是的,它从这样的事情开始。生产中出现错误,打开它们进行调试是个好主意。 –