通过PHP下载PDF获胜't在手机中打开

Downloading PDF via PHP won't open in mobile

本文关键字:手机 下载 PHP PDF 获胜 通过      更新时间:2023-09-26

从wkhtmltopdf生成PDF在桌面上运行良好。我有一个两步的过程来生成文件,然后下载文件。生成的文件在我的移动设备上打开得很好(当我通过电子邮件发送时(,但使用PHP脚本,我无法在下载后打开。

换句话说,造成问题的不是PDF,而是PHP下载。该文件以适当的名称下载,但不会在移动设备中打开。

Galaxy S5带Chrome(最新(。

下载以javascript:开始

window.location = 'savePDF.php?file=' + filename + '&fileName=' + nameString;

下面是PHP脚本:

<?php
$file = $_GET['file'];
header('Pragma: public',true);
header('Expires: 0');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.$_GET['fileName'].'.pdf"');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($file));
readfile($file);
unlink($file);
?>

EDIT:unlink($file)添加到代码中以显示该方面。

想明白了。感谢大家帮助我调试(在没有开发工具的移动设备上,这很痛苦(。

我是readfile($file)之后的unlink($file);,出于某种原因(当它在下载之前询问我想对文件做什么时(,它在读取整个内容之前删除了临时文件。相反,我补充道:

exec("php cleantmp.php $tmpfile > NUL &");

并创建了cleantmp.php:

<?php
if (php_sapi_name() == 'cli') {
    sleep(10);
    unlink($argv[1]);
}
?>

它现在在后台运行,并在10秒后删除临时文件。

再次感谢您的帮助CBroe