2011-04-14 77 views
6

我在使用PHP在Chrome瀏覽器中閱讀PDF文件時遇到問題。Chrome在線PDF中存在「無法加載PDF文檔」的錯誤消息

下面的代碼是我在PHP

$path = "actually file path"; 
header("Pragma: public"); 
header("Expires: 0"); 
header("Content-type: $content_type"); 
header('Cache-Control: private', FALSE); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header("Content-Disposition: inline; filename=\"$filename\""); 
header('Content-Transfer-Encoding: binary'); 
header('Content-Length' . filesize($path)); 
ob_clean(); 
flush(); 
readfile($path); 

怎麼辦。在這裏,我設置的內容配置內聯。因爲如果用戶瀏覽器內置pdf查看器插件,我想顯示pdf文件。您可能知道,Chrome內置pdf查看器。

問題是我有一堆的pdf文件在服務器上。 Chrome瀏覽器只能查看其中的一部分。我無法弄清楚爲什麼其他人不能以相同的方式工作。我已檢查每個文件的權限。它看起來不是權限問題。

有沒有人知道問題是什麼?謝謝。

+2

更改爲強迫下載(內容處置:附件/內容類型:應用程序/八位字節流),下載/保存好PDF和壞的PDF和比較保存到服務器上的內容。 – 2011-04-14 23:21:31

+0

@Marc我嘗試。他們都可以下載並看起來像一樣。我也將它與服務器中的文件進行比較。他們是一樣的。當我切換回Content-disposition:inline。它只是不起作用。 (我甚至比較了響應標題,它們是一樣的) – easycoder 2011-04-14 23:30:34

+0

如果你試圖直接查看一個而不是通過你的腳本,會發生什麼? – 2011-04-14 23:33:13

回答

10

我一直在摔跤這個相同的問題。這和我在瀏覽器中獲得一致的結果一樣接近。我認爲你可能會遇到問題的原因是如果某些PDF太大而無法正確處理readfile()。試試這個:

$file = "path_to_file"; 
$fp = fopen($file, "r") ; 

header("Cache-Control: maxage=1"); 
header("Pragma: public"); 
header("Content-type: application/pdf"); 
header("Content-Disposition: inline; filename=".$myFileName.""); 
header("Content-Description: PHP Generated Data"); 
header("Content-Transfer-Encoding: binary"); 
header('Content-Length:' . filesize($file)); 
ob_clean(); 
flush(); 
while (!feof($fp)) { 
    $buff = fread($fp, 1024); 
    print $buff; 
} 
exit; 
+0

感謝mingala ..會試試看。 – easycoder 2011-04-18 17:34:33

+1

它似乎無法正常工作...... :( – easycoder 2011-04-18 17:52:19

+1

這對於任何瀏覽器或Chrome都不適用嗎?您可以嘗試將緩衝區的大小從1024更改爲小一些如果$ file設置爲文檔的完整路徑,這個解決方案需要設置或刪除 – mingala 2011-04-26 17:46:41

0

我有類似的問題,但我注意到順序很重要。似乎; filename=必須周圍的引號,Content-Disposition: attachment試試這個:

$file = "/files/test.pdf"; 
    $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension 
    $mime = finfo_file($finfo, $file); 

    header('Pragma: public'); 
    header('Expires: 0'); 
    header('Content-Type: $mime'); 
    header('Content-Description: File Transfer'); 
    header('Content-Disposition: attachment; filename="'.basename($file).'"')); 
    header('Content-Transfer-Encoding: binary'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Content-Length' . filesize($file)); 
    ob_clean(); 
    flush(); 
    readfile($file); 
1

我已經解決這樣

$path = 'path to PDF file'; 
header("Content-Length: " . filesize ($path)); 
header("Content-type: application/pdf"); 
header("Content-disposition: inline; filename=".basename($path)); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
ob_clean(); 
flush(); 
readfile($path); 
1

有同樣的問題,Chrome未顯示在線PDF,停留在裝載。解決方法是添加header('Accept-Ranges: bytes')

我的完整代碼:

header('Content-Type: application/pdf'); 
header('Content-Disposition: inline; filename="'.$title.'"'); 
header('Content-Transfer-Encoding: binary'); 
header('Content-Length: '.filesize($file)); 
header('Accept-Ranges: bytes'); 
header('Expires: 0'); 
header('Cache-Control: public, must-revalidate, max-age=0');