Furqan Sehgal
Furqan Sehgal

Reputation: 4997

Obraining SSRS report in the form of pdf

I want to get SSRS report in the form of pdf (or any other portable format) automatically. I mean when user presses button, instead of viewing report in Report View, it should be converted to pdf

Please give me Vb.Net code if possible.

Any help will be welcomed !

Thanks

Upvotes: 0

Views: 1375

Answers (1)

Iain
Iain

Reputation: 6452

Please try this code.

Private Sub GenerateReport(ParamList as hashtable)
        rs.Credentials = System.Net.CredentialCache.DefaultCredentials
        rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials

        Dim historyID As String = Nothing
        Dim deviceInfo As String = Nothing
        Dim format As String = "PDF"
        Dim results As Byte()
        Dim encoding As String = String.Empty
        Dim mimeType As String = String.Empty
        Dim extension As String = String.Empty
        Dim warnings As ReportExecution.Warning() = Nothing
        Dim streamIDs As String() = Nothing
        Dim filename As String = "C:\MyReport.pdf"        ' Change to where you want to save
        Dim _reportName As String = "/Sales/MyReport"  ' Change to be your report
        Dim _historyID As String = Nothing
        Dim _forRendering As Boolean = False
        Dim _values As ReportingService.ParameterValue() = Nothing
        Dim _credentials As ReportingService.DataSourceCredentials() = Nothing
        Dim _parameters As ReportingService.ReportParameter() = Nothing

        _parameters = rs.GetReportParameters(_reportName, _historyID, _forRendering, _values, _credentials)

        Dim ei As ReportExecution.ExecutionInfo = rsExec.LoadReport(_reportName, historyID)
        Dim parameters(_parameters.Length - 1) As ReportExecution.ParameterValue

        for param as integer = 0 to _parameters.count - 1
            parameters(param) = New ReportExecution.ParameterValue
            parameters(param).Label = _parameters(param).name
            parameters(param).Name = _parameters(param).name
            parameters(param).Value = ParamList(param)
        next

        rsExec.SetExecutionParameters(parameters, "en-us")
        results = rsExec.Render(format, deviceInfo, extension, mimeType, encoding, warnings, streamIDs)

        Dim stream As New System.IO.FileStream(filename, IO.FileMode.OpenOrCreate)

        stream.Write(results, 0, results.Length)
        stream.Close()
    End Sub

Upvotes: 1

Related Questions