【发布时间】:2014-04-28 08:31:54
【问题描述】:
我有一个网站,它有两个提交按钮,一个是发送附有 pdf 的电子邮件,这非常有效。
第二个按钮是下载pdf,这就是问题所在。我在下载之前将 pdf 保存在临时文件中,但下载后文件无法打开并且已损坏。 pdf 文件大小约为 30KB。我尝试了类似问题的解决方案,但结果始终相同,pdf 无法打开。
这没用
$fileName = "file.pdf";
$file_name = ("temp/file.pdf");
file_put_contents($file_name, $pdf_content);
$filepath=$file_name; //file location
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);
这没用
$path = $_SERVER['DOCUMENT_ROOT']."/temp/"; // change the path to fit your websites document structure
$fullPath = $path.$fileName;
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment;
filename='.basename($fullPath));
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($fullPath));
readfile($fullPath);
exit;
这没用
set_time_limit(0); // disable timeout
$file = $root_path.'/full-tile-book.pdf';
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="NewName.pdf"');
header('Content-Length: ' . filesize($file));
$f = fopen($file, 'rb');
fpassthru($f);
fclose($f);
exit;
这没用
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/pdf');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($filepath)) . ' GMT');
header('Content-disposition: attachment; filename=file.pdf');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($filepath)); // provide file size
header('Connection: close');
readfile($filepath);
exit();
该文件始终位于服务器上的临时文件夹中,并且工作正常,因此在下载的某处文件已损坏。
我不在乎下载是如何完成的,pdf 或 oclet-stream 或任何其他方式。
【问题讨论】:
-
没关系找到问题,创建 pdf 时出现 php 错误(我看不到它,因为 pdf 无法打开)。 pdf 与电子邮件附件一起使用,但没有下载它,我无法理解,但它现在可以使用!我通过在记事本++中打开pdf发现了错误,如果其他人有问题,他们应该这样做以查看错误。
-
这是一个常见问题。如果将错误发送到浏览器,则可以在呈现 PDF 时在输出流中捕获它们。在使用 dompdf 时,您应该禁用向浏览器发送错误(至少)并使用 PHP 错误日志。
-
这就是我最终所做的。当 pdf 作为电子邮件发送时,错误不存在,所以我认为 pdf 没问题,正在寻找错误的问题。不过现在一切都很好。