Print an SSRS report to disk from Winforms or ASP.NET

This is a short piece of 'proof of concept' code that shows how to print an SSRS report to disk. Our example was coded in a winforms application, but it should work unmodified in ASP.NET.  The ReportParameter allows you to pass parameter(s).

First, load the needed dlls:

In Visual Studio, click on TOOLS > NUGET PACKAGE MANAGER > PACKAGE MANAGER CONSOLE

Then type this in the window:

Install-Package Microsoft.ReportingServices.ReportViewerControl.Winforms

This will install the dlls, and you'll get a 'SQL Server' section in the toolbox. 

The code below is all you'll need:

 
Private Sub GenerateInvoice(InvoiceNum As String)
 
    Try
 
        Dim rv As New Microsoft.Reporting.WinForms.ReportViewer
 
        rv.ServerReport.ReportServerUrl = New System.Uri("http://localhost/reportserver")
        'the report name is 'test', it is in the root folder.
        'if it were in the 'TWO' folder, it would be '/TWO/Test'
        rv.ServerReport.ReportPath = "/Test"
        rv.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Remote
 
        ' Pass a parameter to the report
        Dim myParam As New ReportParameter("InvoiceNum", InvoiceNum)
        rv.ServerReport.SetParameters(myParam)
 
        rv.RefreshReport()
 
        'write the report to disk.
        'this will write to the application directory... which I would not do in production
        Dim content() As Byte
        content = rv.ServerReport.Render("pdf")
 
        'use filestream to save byte array to a file
        Dim o_fs As New IO.FileStream(InvoiceNum & ".pdf", IO.FileMode.Create)
        o_fs.Write(content, 0, content.Length)
        o_fs.Close()
        o_fs = Nothing
 
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
 
End Sub

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences