0

我目前正在使用 wkhtmltopdf 生成 pdf 文件,我想链接最近生成的文件供用户下载,但在用户完成会话后删除它们。无论如何,甚至没有在服务器上生成文件就可以做到这一点?

4

1 回答 1

1
<?php
exec("/path/to/binary/wkhtmltopdf   http://www.google.com /location/test.pdf");
$file = "/location/test.pdf";
$pdf = file_get_contents($file);

header('Content-Type: application/pdf');
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Length: '.strlen($pdf));
header('Content-Disposition: inline; filename="'.basename($file).'";');
ob_clean(); 
flush(); 
echo $pdf;

unlink($file);

?>

文件没有被删除可能存在问题,所以我会添加一个 cron 作业来删除旧文件。

于 2012-08-23T19:51:13.470 回答