【问题标题】:PHP downloaded PDF can open from Ubuntu but not from WindowsPHP下载的PDF可以从Ubuntu打开但不能从Windows打开
【发布时间】:2014-02-24 14:15:50
【问题描述】:

我的服务器上有一个 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');


?>

只是想知道是否有人能明白为什么会发生这种情况。

谢谢!

【问题讨论】:

  • 我发现 Chrome 在标题方面非常非常敏感...我让它以纯文本形式转储 HTML 页面,因为我使用的是 Content-type 而不是 @987654323 @ ...所以我首先尝试将Content-disposition 更改为Content-DispositionContent-type 更改为Content-Type ... 可能无济于事,但不会受到伤害。
  • 感谢您的提示。那似乎没有解决它。很奇怪,我使用 sftp 将 PDF 文件下载到我的 windows 框并打开正常,但是在上面的脚本中使用 PHP 下载时显示错误...

标签: php windows pdf ubuntu download


【解决方案1】:

我找到了解决问题的解决方案的链接:

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();

【讨论】:

    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 2012-04-05
    相关资源
    最近更新 更多