locked
iTextSharp and Cache RRS feed

  • Question

  • User-683743702 posted

    Our policies require we turn the cache off, which is done by the following lines in global.asax

    Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

    Response.Cache.SetCacheability(HttpCacheability.NoCache);<o:p></o:p>

    Response.Cache.SetNoStore();<o:p></o:p>

    Response.AppendHeader("Pragma", "no-cache");<o:p></o:p>

    We also use iTextSharp to take a PDF template on the web server and fille it out then send it to the browser as PDF. It works but we found out above lins cause PDF to be blank. After we comment those lines out, the pdf is fine. Does anybody know why and how to get around it without commenting the no cache out in global file? Thanks in advance.

    Friday, January 28, 2011 2:54 PM

Answers

  • User1520641890 posted

    Our policies require we turn the cache off, which is done by the following lines in global.asax

    Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));

    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    Response.Cache.SetNoStore();

    Response.AppendHeader("Pragma", "no-cache");

    We also use iTextSharp to take a PDF template on the web server and fille it out then send it to the browser as PDF. It works but we found out above lins cause PDF to be blank. After we comment those lines out, the pdf is fine. Does anybody know why and how to get around it without commenting the no cache out in global file? Thanks in advance.

    see the following links for possible causes for the blank pdf:

    if you want to disable caching for .aspx files, you could write a base page class and have all your .aspx pages inherit from it, or use a master page and disable it there. but that could be a lot of work depending on how many pages you have in your application.

    if you must keep global.asax as-is, you could use a simple brute-force approach of filtering out the pages / HTTP handers / other based on file / directory location using Request.Url (or other) from being cached in that manner, and then using the following code in those pages:

    Response.AppendHeader("Expires", "0");
    Response.AppendHeader(
      "Cache-Control",
      "must-revalidate, post-check=0, pre-check=0"
    );
    Response.AppendHeader("Pragma", "public");

    above works for me when sending PDF using iTextSharp. (i work in an environment where HttpCacheability.NoCache over SSL results in a blank PDF for only IE too)

    or you could have all your users switch to Firefox, which doesn't have the problem ;-)


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 29, 2011 1:02 AM

All replies

  • User1520641890 posted

    Our policies require we turn the cache off, which is done by the following lines in global.asax

    Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));

    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    Response.Cache.SetNoStore();

    Response.AppendHeader("Pragma", "no-cache");

    We also use iTextSharp to take a PDF template on the web server and fille it out then send it to the browser as PDF. It works but we found out above lins cause PDF to be blank. After we comment those lines out, the pdf is fine. Does anybody know why and how to get around it without commenting the no cache out in global file? Thanks in advance.

    see the following links for possible causes for the blank pdf:

    if you want to disable caching for .aspx files, you could write a base page class and have all your .aspx pages inherit from it, or use a master page and disable it there. but that could be a lot of work depending on how many pages you have in your application.

    if you must keep global.asax as-is, you could use a simple brute-force approach of filtering out the pages / HTTP handers / other based on file / directory location using Request.Url (or other) from being cached in that manner, and then using the following code in those pages:

    Response.AppendHeader("Expires", "0");
    Response.AppendHeader(
      "Cache-Control",
      "must-revalidate, post-check=0, pre-check=0"
    );
    Response.AppendHeader("Pragma", "public");

    above works for me when sending PDF using iTextSharp. (i work in an environment where HttpCacheability.NoCache over SSL results in a blank PDF for only IE too)

    or you could have all your users switch to Firefox, which doesn't have the problem ;-)


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 29, 2011 1:02 AM
  • User-683743702 posted

    kuujinbo,

    Thank you so much for the information. I'll look into it and see what we can do. Thanks again. 

    Monday, January 31, 2011 7:08 AM