I’m trying to identify if this is an IIS issue or a code issue. I’m leaning toward the former, but in that case I’m not sure how to fix it.
I have several Asp.Net web applications that create PDFs as a MemoryStream (using iTextSharp) and then use the MemoryStream.ToArray method to display the PDF on screen. Below is the basic code I use.
Try
Dim m As System.IO.MemoryStream = pdfhelper.createFileMethod(filename)
Dim data As Byte() = m.ToArray
Response.Clear()
Response.Buffer = True
Response.AddHeader("Content-Length", data.Length.ToString())
Response.AddHeader("Content-Disposition", "inline; filename=sample")
Response.AddHeader("Expires", "0")
Response.AddHeader("Pragma", "cache")
Response.AddHeader("Cache-Control", "private")
Response.ContentType = "application/pdf"
Response.AddHeader("Accept-Ranges", "bytes")
Response.BinaryWrite(data)
Response.Flush()
Response.End()
Catch ex As Exception
throw ex
End Try
I am serving from Windows Server 2008 R2 Standard Edition, 64 bit, version 6.1 Build 7601 Service Pack 1 with IIS 7.5.7600.16385.
When I use the application in Internet Explorer, I have no trouble opening the PDF documents. When I use Chrome or Firefox, the PDF just hangs while loading. It simply will not load. I do not get an error message. The problem is the same whether I run on the server or on the client.
However, when I run on my development box using the IIS built into Visual Studio, I have no problem regardless of browser.
I am aware of the issue addressed in this hotfix. I thus assumed the problem is in IIS. However, I cannot apply the hotfix because it is only valid for the Build 7600 of my version of Windows.
So am back to wondering – is this IIS? Is this my code and it’s just that IE is more forgiving of an error I am making? And how do I fix it?
Thanks for any ideas you can give.
Edit: I found this suggestion, which forces PDFs to be downloaded instead of opened in the browser. Definitely NOT the ideal solution for this problem. Hoping someone has another idea.
I finally resolved this, although I don’t have a 100% “why” for it.
1) I changed my code behind to the following, removing the commands to wait for the buffer and then end response.
2) updated the aspx page for which this is the code behind. It still had all the html “stuff”. I removed everything on the page except for the Page Directive command.
At this point, the PDFs continues to work with IE. On some people’s computers with older (9.x) Adobe plug-ins, Firefox views in the browser with no difficulty. On a computer which has the Adobe 10.1.3, Firefox prompts with the “open or save” dialog. But Chrome (v. 19.0.1084.56) still hangs.
3) After testing with Safari on an IPad and having no issues, I decided it was a Chrome issue. I disabled the Chrome Adobe plug-in and now the PDF displays in the browser as expected.