Render the contents of a ReportViewer control directly to PDF.

Rendering the output of a ReportViewer control (SQL Reporting Services) directly to PDF is fairly easy. It involves simply rendering the Report Viewer output to a byte array, and then pushing it to a MemoryStream object and writing to the Output Stream. Code below:

Dim warnings As Warning() = Nothing

Dim streamids As String() = Nothing

Dim mimeType As String = Nothing

Dim encoding As String = Nothing

Dim extension As String = Nothing

Dim bytes As Byte()

bytes = ReportViewer1.ServerReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)

Dim
ms As New System.IO.MemoryStream(bytes)

Response.ContentType = "Application/pdf" 

Response.BinaryWrite(ms.ToArray())

Response.End()

2 Comments

  • There's no point in creating a new MemoryStream and calling ToArray - that simply creates a copy of the bytes array. Just call Response.BinaryWrite(bytes) instead.

  • You're absolutely right. I'll update the code. In the actual app I wrote, I used an external PDF object to do some manipulation, and as I remember it, that required the second object.

    Thanks!

Comments have been disabled for this content.