如何使 FPDF 生成的 pdf 不可缓存?
我正在检查一个用原始 php 编写的应用程序,没有任何框架。在模块中,有使用 FPDF 生成的报告并且工作正常,只是 pdf 被缓存了。生成例程的原始调用是
<a href="tr_inci_print.php" target="_new">Print</a>
tr_inci_print.php 使用 2 个参数,存储在会话中的年份和月份。我已经更改了代码,
<a href="tr_inci_print.php?anio=<?php echo $anio; ?>&mes=<?php echo $mes; ?>" target="_new">Imprimir</a>
部分解决了问题,因为 URI 每个月都在变化。但是,如果数据在外部发生更改并且浏览器仍位于原始页面中,则重新调用链接确实会生成未更新的 pdf。
有什么方法可以更改 $FPDF->output() 以使 pdf 不可缓存?
------ 部分解决方案 -------------------
按照 oezi 的回答,进行了更改:
$oPdf->output()
解决
$buffer=$oPdf->Output('','S');
header('Content-Type: application/pdf');
header('Content-Length: '.strlen($buffer));
header('Content-Disposition: inline; filename="doc.pdf"');
header("Cache-Control: no-cache, must-revalidate, max-age=1"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // any date in the past
header('Pragma: public');
ini_set('zlib.output_compression','0');
echo $buffer;
了 Chrome 和 IE 中的问题,但解决了 FireFox 4 中的问题。
I'm checking an app written in raw php, without any framework. In a module there are reports generated with FPDF and working fine, except that the pdfs gets cached. The original invocation of the generation routine was
<a href="tr_inci_print.php" target="_new">Print</a>
tr_inci_print.php uses 2 parameters, year and month that are stored in session. I've changed the code to
<a href="tr_inci_print.php?anio=<?php echo $anio; ?>&mes=<?php echo $mes; ?>" target="_new">Imprimir</a>
that solves partially the problem, because of the URI changing in every month. But if data changes externally and the browser is still in the original page, reinvoking the link does generates an not updated pdf.
There is any way to change $FPDF->output() to make the pdf non cacheable?
------ Partial solution -------------------
following oezi answer, changed :
$oPdf->output()
with
$buffer=$oPdf->Output('','S');
header('Content-Type: application/pdf');
header('Content-Length: '.strlen($buffer));
header('Content-Disposition: inline; filename="doc.pdf"');
header("Cache-Control: no-cache, must-revalidate, max-age=1"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // any date in the past
header('Pragma: public');
ini_set('zlib.output_compression','0');
echo $buffer;
that solved the problem in Chrome and IE, but not in FireFox 4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需设置一个标头即可实现此目的,只需将以下行添加到您的
tr_inci_print.php
中:编辑:请务必在之前添加此行< /em> 调用
output()
you just have to set a header to achive this, just add the folowing lines to your
tr_inci_print.php
:EDIT: please be sure to add this lines before calling
output()
尝试:
编辑:添加 Javascript 以确保 pdf 是最新的,即使不重新加载主页:
try:
EDIT: adding Javascript to make sure the pdf is fresh even without reloading the main page: