Serving PDF report as HTTP stream into web browser

I've been looking into achieving this capability with minimum amount of code for sometime; Using URL access in sQL Server Reporting Services to render the file in browser without having a save as popup nor using the Report Viewer Control. Just a plain old binary data streaming into broswer IOStream. I tried WebClient but it didn't work very well; For fine grained control on timing, since report generation could take longer, HTTPWebRequest and HTTPWebResponse are probably the best bet here.

Dim ReportUrl As String = "http:///ReportServer?%2f%&rs%3aCommand=Render&rs%3AFormat=PDF"

Dim ReportWebRequest As HttpWebRequest = CType(WebRequest.Create(ReportUrl), HttpWebRequest)
ReportWebRequest.Timeout = 10000
ReportWebRequest.Credentials = CredentialCache.DefaultCredentials

Dim ReportWebResponse As HttpWebResponse = CType(ReportWebRequest.GetResponse(), HttpWebResponse)
Dim ReportResponseStream As StreamReader = New StreamReader(ReportWebResponse.GetResponseStream(), New
Dim objMemoryStream As New MemoryStream(New UnicodeEncoding().GetBytes(ReportResponseStream.ReadToEnd()))

Response.AddHeader("Accept-Header", objMemoryStream.Length.ToString())
Response.ContentType = "application/pdf"
Response.OutputStream.Write(objMemoryStream.ToArray(), 0, Convert.ToInt32(objMemoryStream.Length))

End Try


2 thoughts on “Serving PDF report as HTTP stream into web browser

  1. Thanks much! This worked great for me. As I was getting these PDFs 'scraped' from another web site, I also added code to cache these in a local database and only retrieve the Http Stream from the site if I lacked it in the SQL database.

    I appreciate you taking the time to provide this code. It worked the first time for me.

    Kind Regards,
    Damon Carr

Comments are closed.