Monday, February 9, 2009

Saving PDF reports from SQL Server Reporting Services using VB.NET

While there are plenty of C# examples, there are surprisingly few *working* examples of vb.net code to print reports from Reporting Services via code. Hopefully this will help to fill that void. This sample uses SSRS 2005 and VB.NET and should prove to be a welcome change from using third-party com objects for generating pdf files.



Dim strCreateUrl As String = yourServer + yourReportFolder + "?" + yourReportName + "&rs:Command=Render&rs:format=PDF"

Dim webRequest As System.Net.WebRequest = System.Net.WebRequest.Create(strCreateUrl)
webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials

' Return the response.
Dim webResponse As System.Net.HttpWebResponse = CType(webRequest.GetResponse(), System.Net.HttpWebResponse)
Dim ReceiveStream As System.IO.Stream = webResponse.GetResponseStream()

Dim binByte As Byte()
Dim rdBinaryReader As BinaryReader = New BinaryReader(webResponse.GetResponseStream)
binByte = rdBinaryReader.ReadBytes(webResponse.ContentLength)
rdBinaryReader.Close()

Dim outstream As New MemoryStream()
outstream.Write(binByte, 0, binByte.Length)

Dim buffer(binByte.Length) As Byte
Using stream As FileStream = File.OpenWrite(outputName)

stream.Write(binByte, 0, binByte.Length)
webResponse.Close()

End Using

No comments:

Post a Comment