如何链接用户在当前会话期间使用wkhtmltopdf生成的要下载的文件


How do I link files to download that were generated by a user using wkhtmltopdf during their current session

我目前正在使用wkhtmltopdf生成pdf文件,我想链接最近生成的文件供用户下载,但在用户完成会话后将其删除。是否有任何方法可以在不需要在服务器上生成文件的情况下实现这一点?

<?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作业来删除旧文件。