BUG: Small compressed (gzip or deflate) PDF files served inline are "damaged".

G

Guest

Internet Explorer 6.0.2800.1106.xpsp2.030422-163

I have an ASP.NET application that provides access to PDF reports stored in zip files on the server
The compressed data is sent to the browser using the "Content-Encoding" header to tell the browser
it needs to decompress the data. When the user selects a report it is displayed in the browser by
the Adobe Reader plugin

This works fine for larger PDFs (over about 48k bytes uncompressed) but fails with smaller PDFs by
either freezing up or by displaying the message: "The file is damaged and could not be repaired.

I suspect that the data is being decompressed in 48k chunks and the problem occurs when the file is
small enough to decompress in one chunk. Maybe the browser is telling the plugin it has the full 48
of data and the plugin gets lost or throws a fit when it can't find "%%EOF" at the end of the data

The same files are handled fine if saved to a file or opened in the external Adobe Reader by using
Right-Click/Save-As on the link, or by changing the Content-Disposition from "inline" to "attachment",
or by unchecking the Adobe option "Display PDF in browser"

A workaround would be to decompress the smaller files on the fly at the server and send them without
the "Content-Encoding" header while continuing to send the larger files in their compressed state

The pertinent ASP.NET code serving the PDF data

FileStream fs = new FileStream(ZipName, FileMode.Open, FileAccess.Read)
BinaryReader br = new BinaryReader(fs)
br.BaseStream.Seek(FileOffset, SeekOrigin.Begin)

byte[] hdr = {0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0b}
byte[] crc = BitConverter.GetBytes(Crc)
byte[] uncomplen = BitConverter.GetBytes(UncompLen)

MemoryStream ms = new MemoryStream()
ms.Write(hdr, 0, hdr.Length)
ms.Write(br.ReadBytes(FileSize), 0, FileSize)
ms.Write(crc, 0, 4)
ms.Write(uncomplen, 0, 4)

Response.Clear()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Type", "application/pdf")
Response.AddHeader("Content-Disposition", "inline;filename=" + ReptKey + ".pdf")
Response.AddHeader("Content-Encoding", "gzip")
Response.BinaryWrite(ms.ToArray())
Response.End()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top