我試圖在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
是的,它從這樣的事情開始。生產中出現錯誤,打開它們進行調試是個好主意。 –