How to make an FPDF's generated pdf non cacheable?
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 $开发者_StackOverflow中文版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.
you just have to set a header to achive this, just add the folowing lines to your tr_inci_print.php
:
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // any date in the past
EDIT: please be sure to add this lines before calling output()
try:
<a href="tr_inci_print.php?t=<?php echo time(); ?>" target="_new">Print</a>
EDIT: adding Javascript to make sure the pdf is fresh even without reloading the main page:
<a href="tr_inci_print.php?t=<?php echo time(); ?>"
onclick="document.location.href='tr_inci_print.php?t='+new Date().getTime(); return false;" >
Print
</a>
精彩评论