Garfield
Garfield

Reputation: 1222

IHttpHandler Response.ContentType = "application/pdf" broken in IE8

This works in Chrome and Firefox, but IE8 displays nothing... When I tried the same code in a webforms button click it works on all three browsers.

How can I get this to work in IE8?

public class ShowPDF : IHttpHandler
{

   public void ProcessRequest(HttpContext context)
   {
       // create PDF document
       var document = new PdfDocument();
       var page = document.AddPage();
       var font = new XFont("Verdana", 20, XFontStyle.Bold);
       var gfx = XGraphics.FromPdfPage(page);
       gfx.DrawString("Hello, World!", font
           , PdfSharp.Drawing.XBrushes.Black
           , new PdfSharp.Drawing.XRect(0, 0, page.Width, page.Height)
           , PdfSharp.Drawing.XStringFormats.Center
       );

       // Send PDF to browser
       var stream = new System.IO.MemoryStream();
       document.Save(stream, false);
       context.Response.Clear();
       context.Response.ContentType = "application/pdf";
       context.Response.AddHeader("content-length", stream.Length.ToString());
       context.Response.BinaryWrite(stream.ToArray());
       context.Response.Flush();
       stream.Close();
       context.Response.End();
   }

   public bool IsReusable
   {
       get
       {
           return false;
       }
   }
}

Upvotes: 3

Views: 32124

Answers (1)

Garfield
Garfield

Reputation: 1222

Solved! It came down to browser configuration.

@BrianRogers - Thanks for testing this. I tried "window.location.href = 'ShowPDF.ashx';" as you did and IE8 displayed a blank page. This caused me to question my browser configuration. I uninstalled Foxit Reader and installed Adobe Reader. Now all works as expected.

The confusing part was when I put the code to render the pdf in an aspx server side button click, IE8 displayed the PDF just fine! Go figure! For that reason I wasn't questioning my browser configuration earlier.

Upvotes: 2

Related Questions