maddob
maddob

Reputation: 1009

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

Upvotes: 3

Views: 4433

Answers (2)

Roy
Roy

Reputation: 4464

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

<iframe src="file.pdf?=<?=time();?>"></iframe>

Upvotes: 4

maddob
maddob

Reputation: 1009

After many hours of trying, the solution I found is to really generate a new file each time (Solution 3 from the question without removing the random string at the end of the file). As a result it was necessary to update the database and to delete the old files on every change. My initial intention was to avoid these actions, but unfortunately no other solution was found

Upvotes: 0

Related Questions