Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable browser cache for displayed in an iFrame PDF by means of TCPDF

I am trying for hours to solve the following caching problem.

My application has the following structure (simplified):

  1. index.php - main page (contains various input fields, submit button and an iframe for dispaying PDF content with the help of TCPDF)

  2. generate.php - generates PDF file based on the supplied POST parameters and stores the file to the filesystem

  3. viewer.php - Displays the PDF document (TCPDF libraries). The iframe loads this script to show the pdf file

The workflow is pretty simple - the user chooses some options and clicks the submit button on the main page. The selected parameters are sent per AJAX by POST to the generate.php script. The script generates the PDF file and stores it to the filesystem. At the end it returns the newly created/edited filename. The filename is fetched in the AJAX callback function, which then refreshes the iframe with the new/edited filename:

viewer.php?filename=NEW_OR_EDITED_FILENAME

Everything is working, but when the file is being replaced, sometimes (NOT ALWAYS), the browser shows the old pdf file, although the new version is on the hard drive. I tried the following solutions:

  1. Add Meta tags to disable cache to the generated HTML by index.php and viewer.php
  2. Disabling cache for jQuery AJAX calls by: jQuery.ajaxSetup({cache: false});
  3. Adding some random string to the the filename parameter:

    viewer.php?filename=FILENAME_RANDOMSTRING

The RANDOMSTRING is then removed from the script and the filename is extracted. None of these solutions worked for me. Tested browsers are: Chrome 25.0.1364.152 and Firefox 19.0. Can someone help me with this? Thanks in advance

like image 919
maddob Avatar asked Dec 29 '25 18:12

maddob


1 Answers

Just had the same problem but after adding a random string it works perfect:

<iframe src="file.pdf?=<?=time();?>"></iframe>
like image 142
Roy Avatar answered Jan 01 '26 22:01

Roy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!