簡體   English   中英

PHP下載的PDF可以從Ubuntu打開,但不能從Windows打開

[英]PHP downloaded PDF can open from Ubuntu but not from Windows

我的服務器上有一個PHP腳本,可以創建和下載PDF文件。 我已經從我的Ubuntu桌面計算機上的Chromium客戶端進行了測試,它可以下載並正常打開。

當我嘗試從Windows 7和Windows 8 Chrome瀏覽器下載文件時,但是系統無法打開該文件。 錯誤顯示為

“它不是受支持的類型,或者它已損壞...”。

這是提示下載的PHP代碼:

<?php
...
ob_start();

include 'show_report.php';

$content = ob_get_clean();

$tmpfname = tempnam("/var/www/phpAJAX/Reports/", "pdf_");
$handle = fopen($tmpfname, "w");
fwrite($handle, $content);
fclose($handle);



shell_exec('mv '.$tmpfname.' '.$tmpfname.'.html');
$output = shell_exec('wkhtmltopdf --page-size Letter '.$tmpfname.'.html '.$tmpfname.'.pdf');

//echo $content;

header("Content-disposition: attachment; filename=".$_GET['report_name'].".pdf");
header("Content-type: application/pdf");
readfile($tmpfname.'.pdf');


?>

只是想知道是否有人可以看到為什么會這樣。

謝謝!

我找到了解決此問題的解決方案的鏈接:

http://board.phpbuilder.com/showthread.php?10372951-RESOLVED-Can-t-open-pdf-file-downloaded-via-php-header

如果將標頭更改為以下PHP代碼,則PDF文件正確下載並在Windows和Ubuntu上正確打開。

header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename="FILENAME"'); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize("PATH/TO/FILE")); 
ob_clean(); 
flush(); 
readfile(PATH/TO/FILE);      
exit();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM