Generate a Report in PDF format
It is sometimes necessary to generate reports in distributable formats. The following example uses two SQL Services Reporting Services (SSRS) Web services to execute a report and export it in the Adobe PDF format. This example uses a web application to write the resulting PDF file, allowing you to either view or save the report. The standard Process Overview report is used in the example.
The Web service references are made to the ReportService2010.asmx and the ReportExecution2005.asmx Web services, which are typically located on the Report Manager Web site. For example, http://<servername>/ReportServer/ReportService2010.asmx and http://<servername>/ReportServer/ReportExecution2005.asmx. In the example below, the name given to the first reference is rs2010 and the name given to the second service is rsExecService.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default: System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
public void GenerateReportPDF(string ReportName, string ReportFormat) {
rs2010.ReportingService2010 rs = new rs2010.ReportingService2010();
rsExecService.ReportExecutionService rsExec = new rsExecService.ReportExecutionService();
rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
string historyID = null;
string deviceInfo = null;
string format = ReportFormat;
Byte[] results;
string encoding = String.Empty;
string mimeType = String.Empty;
string extension = String.Empty;
rsExecService.Warning[] warnings = null;
string[] streamIDs = null;
string filename = string.Empty;
switch (ReportFormat) {
case "PDF":
filename = @ "Report.PDF";
break;
case "IMAGE":
filename = @ "Report.Tiff";
break;
default:
break;
}
string _reportName = ReportName;
string _historyID = null;
bool _forRendering = false;
rs2010.ParameterValue[] _values = null;
rs2010.DataSourceCredentials[] _credentials = null;
rs2010.ItemParameter[] _parameters = null;
try {
_parameters = rs.GetItemParameters(_reportName, _historyID, _forRendering, _values, _credentials);
rsExecService.ExecutionInfo ei = rsExec.LoadReport(_reportName, historyID);
rsExecService.ParameterValue[] parameters = new rsExecService.ParameterValue[1];
if (_parameters.Length > 0) {
parameters[0] = new rsExecService.ParameterValue();
parameters[0].Name = "ProcessSetID";
}
results = rsExec.Render(format, deviceInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.AddHeader("Content-Length", results.Length.ToString());
Response.ContentType = "application/octetstream";
Response.BinaryWrite(results);
Response.Flush();
Response.End();
} catch (Exception ex) {
}
}
protected void btnRunReport_Click(object sender, EventArgs e) {
GenerateReportPDF("/Standard Reports/Process Overview", "PDF");
}
}