Enable pdf caching in IE
-
05-07-2019 - |
Question
At first, caching didn't work in all browsers. Then I made it work in all browsers but IE (IE8) by adding .pdf extension to the url. Servlet stopped being called after that.
I display pdf file inline on the webpage via EMBED tag that loads the following url:
http://localhost:7001/app/viewFile.pdf
Which is generated by java servlet with the following headers:
response.addHeader("Content-Disposition", "inline;");
response.setHeader("Cache-control", "cache,max-age=600");
response.setContentType(mimeType);
response.setContentLength(contentLength);
For pdf displaying in all browsers I use Adobe Reader 9.2.0.
How to make it work in IE too? I noticed that IE adds 'Cache-Control: no-cache' header to request, whereas Safari, for example, doesn't.
Solution
As said before, the cache-control
header value cache
is invalid. Use public
instead.
As to IE not adhering the server-side cache control rules in the embed
and object
elements, this is unfortunately a "feature" of IE. Best what you can do is replacing it by an iframe
element.
Other headers like expires
, last-modified
, etag
and so on ain't going to help.
OTHER TIPS
Some ideas to look into:
I don't think
cache
is a validCache-Control
directive.Try using a value of
public
instead, orprivate
if that's more appropriate to your content. Check out RFC 2616 for more information.Perhaps you're sending more than one
Cache-Control
directive?Use a tool like Firebug or LiveHTTPHeaders to peek at the actual headers your browsers are receiving. Make sure they're not getting something you don't expect. It sounds like you might already be doing this though. Also make sure you're not also sending
Pragma: no-cache
.Try setting the
Expires
header in addition to usingCache-Control
.It's also possible you're sending the browser conflicting
Cache-Control
/Pragma
headers and IE chooses to take thePragma
headers as first priority for whatever backwards reason.Make sure IE is configured to allow caching! :)
Control Panel
>Internet Options
>Temporary Internet Files
>Settings
>Check for newer versions of stored pages
Try sending the PDF as a response to a
POST
request (via form submit).IE is allowing a cache to take place regardless of the headers in the response due to this requirement from RFC 2616: "By default, a response is cacheable if the requirements of the request method, request header fields, and the response status indicate that it is cacheable." Responses to
POST
requests are NOT cacheable, so IE shouldn't include that header in it's request.Try sending the
Content-MD5
andLast-Modified
headers with consistent values (if they're not already being sent).This might help convince IE that the content of the PDF has not changed. I don't think this solution will work, because IE is obviously stubborn, but it's worth mentioning.
Well, one obvious way around the problem is to use URL rewriting. If IE works with .pdf in the extension, use mod_rewrite (Apache) or a similar tool to server-side redirect to the right page, while making the client think that it's indeed requesting a PDF file.
Also: review the HTTP headers that the client is receiving using a tool like Fiddler.
Also: review this older question (PHP: Force file download and IE, yet again), I've had similar problems with IE not forcing downloads, too.