问题
Language : ASP.NET/VB.NET
I am creating pdf file using itextsharp library. On my development machine generated files are opening up without any problem. But when I upload the compiled program on production server, internet explorer just open and close the window but does not show the pdf file. Other browsers are showing up the pdf file.
Here is the code I am using
Dim LABNO As Integer = Request.QueryString("LABNO")
Dim vDoc As New iTextSharp.text.Document(PageSize.A4, 20, 20, 50, 50)
Dim vOutPut As New IO.MemoryStream
Dim vWriter = pdf.PdfWriter.GetInstance(vDoc, vOutPut)
Dim cb As New PdfContentByte(vWriter)
MakeReceipt(LABNO, vDoc, cb)
vDoc.NewPage()
MakeReceipt(LABNO, vDoc, cb)
'MakeSheet(LABNO)
vDoc.Close()
Dim rnd As New Random
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "Application/pdf"
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=Rct-{0}.pdf", rnd.Next.ToString()))
Response.BinaryWrite(vOutPut.ToArray())
Response.Flush()
回答1:
This problem (due to the way MSIE accepts bytes) is explained in the "iText in Action" books. You can solve it by adding an extra header: Content-Length
(the value is the number of bytes in vOutPut.
Why does this problem occur? If you don't tell MSIE how many bytes to expect, it will download blocks of a specific size (e.g. 1028 bytes at a time). If the size of your PDF isn't an exact multiple of this size, then extra (garbage) characters will be added at the end. Some PDF viewers can't deal with these extra characters.
Based on experience I also add the following headers:
"Expires", "0"
"Cache-Control", "must-revalidate, post-check=0, pre-check=0"
"Pragma", "public"
I don't know if they are really necessary, but they don't hurt.
来源:https://stackoverflow.com/questions/12473356/pdf-file-not-showing-up-in-internet-explorer