[英]PHP downloaded PDF can open from Ubuntu but not from Windows
I have a PHP script on my server which creates and downloads a PDF file. 我的服务器上有一个PHP脚本,可以创建和下载PDF文件。 I have tested it out from a Chromium client on my Ubuntu Desktop computer, and it downloads and opens fine.
我已经从我的Ubuntu桌面计算机上的Chromium客户端进行了测试,它可以下载并正常打开。
When I try from windows 7 and windows 8 chrome browsers, the file downloads but then the system is unable to open the file. 当我尝试从Windows 7和Windows 8 Chrome浏览器下载文件时,但是系统无法打开该文件。 The error reads,
错误显示为
"it is either not a supported type or it has been damaged...".
“它不是受支持的类型,或者它已损坏...”。
Here is the PHP code that prompts the download: 这是提示下载的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');
?>
Just wondering if someone can see why this might be happening. 只是想知道是否有人可以看到为什么会这样。
Thanks! 谢谢!
I found a link with a solution that fixed the problem: 我找到了解决此问题的解决方案的链接:
http://board.phpbuilder.com/showthread.php?10372951-RESOLVED-Can-t-open-pdf-file-downloaded-via-php-header http://board.phpbuilder.com/showthread.php?10372951-RESOLVED-Can-t-open-pdf-file-downloaded-via-php-header
If you change the headers to the following PHP code, then the PDF file downloaded correctly and opened correctly on windows and ubuntu. 如果将标头更改为以下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.