Generate SQL Server Reporting Services (SSRS) Report as PDF from URL with VB.NET or C#.NET
Generating an existing SQL Server Reporting Services (SSRS) report to a PDF via a web URL is a convenient way to distribute SSRS reports, invoices, or anything else from SSRS. This post focuses on an existing report; however, links to the basic set up for a report are included. The code to output an existing report as PDF are given and uses a ReportViewer control.
You can skip to the PDF Output section if your server is all set up with the ReportViewer controls.
The pieces and parts to output an SSRS report as a PDF were culled from several disparate sources. All sources are given appropriate credit for their work.
As an added bonus, VB.NET and C#.NET versions are shown.
Add Report Viewer Redistributable to Server
The ReportViewer controls need to be installed on the server and Microsoft provides the redistributable for that in their download center.
The Microsoft Report Viewer 2008 Redistributable Package includes Windows Forms and ASP.NET Web server controls for viewing reports designed using Microsoft reporting technology.
Add HTTP Handler Web Server
The server has to know what it is dealing with when it is asked to generate a ReportViewer control. To help the server out, add the appropriate handlers.
Brite Global provides some excellent instructions on how to add the handlers.
Create Report on SSRS Server
The “Who Needs SQL Server’s Reporting Services?” article by DataSprings is an excellent tutorial for getting started with your first report.
Add Generic User to Report Server
For security reasons, a generic user will need to be added to the SSRS server. These credentials will be used by the .aspx page to access the report.
Give this user “Browser – May view folders, reports and subscribe to reports” only rights.

This generic (aka anonymous) user’s credentials will be stored in a separate config file and pulled into the web.config. That way if they change, you are not mucking about in the web.config file.
Create a appSetting.config and add the following information:
< add key = "rvUser" value = "GENERIC-USERNAME" /> |
< add key = "rvPassword" value = "GENERIC-PASSWORD" /> |
< add key = "rvDomain" value = "YOUR-REPORTSERVER-DOMAIN" /> |
Pop it into the web.config as such:
< appSettings file = "appSettings.config" > |
<!-- Could have other keys here --> |
The authorization is handled by using the IReportServerCredentials Interface which “allows applications to provide credentials for connecting to a Reporting Services report server.”
PDF Output
Add the ReportViewer control the an .aspx page by adding an assembly reference and the control itself:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="YOUR-FILE_NAME.vb" Inherits="YOUR-INHERITS" %> |
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" |
Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %> |
< form id = "form1" runat = "server" > |
< rsweb:ReportViewer ID = "ReportViewer1" runat = "server" > |
The code behind page got a lot of help from Viral at Beyond Relational’s post “Generating and exporting SSRS reports programatically using Report Viewer Control” and using Microsoft’s IReportServerCredentials Interface that was mentioned earlier:
VB.NET
Imports System.Security.Principal |
Imports Microsoft.Reporting.WebForms |
Partial Class YOUR_PAGE_CLASS |
Inherits System.Web.UI.Page |
Protected Sub Page_Load( ByVal sender As Object , ByVal e As System.EventArgs) Handles Me .Load |
Private Sub SetReportParameters() |
ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote |
' Set report server and report path |
ReportViewer1.ServerReport.ReportServerUrl = _ |
ReportViewer1.ServerReport.ReportPath = _ |
Dim paramList As New Generic.List(Of ReportParameter) |
Dim pInfo As ReportParameterInfoCollection |
pInfo = ReportViewer1.ServerReport.GetParameters() |
'if you have report parameters - add them here |
paramList.Add( New ReportParameter( "PARAM1-EXAMPLE" , "1" , True )) |
paramList.Add( New ReportParameter( "PARAM2-EXAMPLE" , "2" , True )) |
ReportViewer1.ServerReport.SetParameters(paramList) |
' Process and render the report |
ReportViewer1.ServerReport.Refresh() |
Dim returnValue As Byte () |
Dim format As String = "PDF" |
Dim deviceinfo As String = "" |
Dim mimeType As String = "" |
Dim encoding As String = "" |
Dim extension As String = "pdf" |
Dim streams As String () = Nothing |
Dim warnings As Microsoft.Reporting.WebForms.Warning() = Nothing |
returnValue = ReportViewer1.ServerReport.Render(format, deviceinfo, mimeType, encoding, extension, streams, warnings) |
Response.ContentType = mimeType |
Response.AddHeader( "content-disposition" , "attachment; filename=YOUR-OUTPUT-FILE-NAME.pdf" ) |
Response.BinaryWrite(returnValue) |
Protected Sub Page_Init( ByVal sender As Object , _ |
ByVal e As System.EventArgs) _ |
ReportViewer1.ServerReport.ReportServerCredentials = _ |
New MyReportServerCredentials() |
Public NotInheritable Class MyReportServerCredentials |
Implements IReportServerCredentials |
Public userName As String = ConfigurationManager.AppSettings( "rvUser" ) |
Public password As String = ConfigurationManager.AppSettings( "rvPassword" ) |
Public domain As String = ConfigurationManager.AppSettings( "rvDomain" ) |
Public ReadOnly Property ImpersonationUser() As WindowsIdentity _ |
Implements IReportServerCredentials.ImpersonationUser |
'Use the default windows user. Credentials will be |
'provided by the NetworkCredentials property. |
Public ReadOnly Property NetworkCredentials() As ICredentials _ |
Implements IReportServerCredentials.NetworkCredentials |
'Read the user information from the web.config file. |
'By reading the information on demand instead of storing |
'it, the credentials will not be stored in session, |
'reducing the vulnerable surface area to the web.config |
'file, which can be secured with an ACL. |
If ( String .IsNullOrEmpty(userName)) Then |
Throw New Exception( "Missing user name from web.config file" ) |
If ( String .IsNullOrEmpty(password)) Then |
Throw New Exception( "Missing password from web.config file" ) |
If ( String .IsNullOrEmpty(domain)) Then |
Throw New Exception( "Missing domain from web.config file" ) |
Return New NetworkCredential(userName, password, domain) |
Public Function GetFormsCredentials( ByRef authCookie As Cookie, _ |
ByRef userName As String , _ |
ByRef password As String , _ |
ByRef authority As String ) _ |
Implements IReportServerCredentials.GetFormsCredentials |
'Not using form credentials |
C#
using System.Configuration; |
using System.Security.Principal; |
using Microsoft.Reporting.WebForms; |
partial class YOUR_PAGE_CLASS : System.Web.UI.Page |
public string SSRS = ConfigurationManager.AppSettings[ "SSRS" ]; |
protected void Page_Load( object sender, System.EventArgs e) |
private void SetReportParameters() |
//ReportViewer1.ProcessingMode = ProcessingMode.Remote |
ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote; |
// Set report server and report path |
ReportViewer1.ServerReport.ReportServerUrl = new Uri(SSRS); |
ReportViewer1.ServerReport.ReportPath = "/YOUR-REPORT-PATH" ; |
System.Collections.Generic.List<ReportParameter> paramList = new System.Collections.Generic.List<ReportParameter>(); |
ReportParameterInfoCollection pInfo = default (ReportParameterInfoCollection); |
pInfo = ReportViewer1.ServerReport.GetParameters(); |
//if you have report parameters - add them here |
paramList.Add( new ReportParameter( "PARAM1-EXAMPLE" , "1" , true )); |
paramList.Add( new ReportParameter( "PARAM1-EXAMPLE" , "2" , true )); |
ReportViewer1.ServerReport.SetParameters(paramList); |
// Process and render the report |
ReportViewer1.ServerReport.Refresh(); |
byte [] returnValue = null ; |
string extension = "pdf" ; |
Microsoft.Reporting.WebForms.Warning[] warnings = null ; |
returnValue = ReportViewer1.ServerReport.Render(format, deviceinfo, out mimeType, out encoding, out extension, out streams, out warnings); |
Response.ContentType = mimeType; |
Response.AddHeader( "content-disposition" , "attachment; filename=YOUR-OUTPUT-FILE-NAME.pdf" ); |
Response.BinaryWrite(returnValue); |
protected void Page_Init( object sender, System.EventArgs e) |
ReportViewer1.ServerReport.ReportServerCredentials = new MyReportServerCredentials(); |
public sealed class MyReportServerCredentials : IReportServerCredentials |
public string userName = ConfigurationManager.AppSettings[ "rvUser" ]; |
public string password = ConfigurationManager.AppSettings[ "rvPassword" ]; |
public string domain = ConfigurationManager.AppSettings[ "rvDomain" ]; |
public WindowsIdentity ImpersonationUser |
//Use the default windows user. Credentials will be |
//provided by the NetworkCredentials property. |
public ICredentials NetworkCredentials |
//Read the user information from the web.config file. |
//By reading the information on demand instead of storing |
//it, the credentials will not be stored in session, |
//reducing the vulnerable surface area to the web.config |
//file, which can be secured with an ACL. |
if (( string .IsNullOrEmpty(userName))) |
throw new Exception( "Missing user name from web.config file" ); |
if (( string .IsNullOrEmpty(password))) |
throw new Exception( "Missing password from web.config file" ); |
if (( string .IsNullOrEmpty(domain))) |
throw new Exception( "Missing domain from web.config file" ); |
return new NetworkCredential(userName, password, domain); |
public bool GetFormsCredentials( out Cookie authCookie, |
out string userName, out string password, |
// Not using form credentials |
Now just run your URL in a browser (http://YOUR-DOMAIN.com/YOUR-PAGE.aspx) and the File Download warning box will pop up for the PDF.
PDF Layout Tips and Things to Look Out For
- Set Report PageSize to 8.5in, 11in for portrait or 11in, 8.5in for landscape.
- Set the Report Margins as you would like.
- Make sure your Report Body (white staging area in Visual Studio) does not exceed your Report PageSize minus your margins.
- Remove any unused white space in Report Body no matter what you set the sizes at.
Reff: http://www.jensbits.com/2012/01/23/generate-sql-server-reporting-services-ssrs-report-as-pdf-from-url-with-vb-net-or-c-net/
Like this:
Like Loading...
Related