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) {

 }

 /// <summary>
 /// It is sometimes necessary to generate reports in distributable formats. The following example
 /// uses two SQL Server Reporting Services (SSRS) Web services to execute a report and export it
 /// in the Adobe PDF format. The example below uses a web application to write the resulting PDF
 /// file to the response back to the client, allowing them to either view or save the report. The
 /// standard K2 Process Overview report is used in the example.
 /// NOTE: More information about using this code and consuming Report Server Web Service
 /// Endpoints can be found at msdn.microsoft.com.
 /// </summary>
 /// <param name="ReportName">Path to the Report in SSRS e.g. "/Standard Reports/Process Overview" </param>
 /// <param name="ReportFormat">PDF, IMAGE, etc. Based on Reporting Services export options</param>
 public void GenerateReportPDF(string ReportName, string ReportFormat) {
  // NOTE: Setup web references to the following SSRS web service endpoints, based on SQL Server 2012. See web.config as well.
  // rs2020.ReportingService2010: http://<Server Name>/ReportService2010.asmx?wsdl
  // rsExecService.ReportExecutionService: http://<Server Name>/ReportExecution2005.asmx?wsdl
  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];

   // Place to include the parameter.
   if (_parameters.Length > 0) {
    parameters[0] = new rsExecService.ParameterValue();
    parameters[0].Name = "ProcessSetID";
    // parameters[0].Value = null;
   }

   // Sets and validates parameter values associated with the current report execution.
   // if report parameters are read-only do not execute SetExecutionParameters
   //rsExec.SetExecutionParameters(parameters, "en-us");

   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) {
   // Handle exceptions accordingly
  }
 }

 protected void btnRunReport_Click(object sender, EventArgs e) {
  // Pass in path to the report you want from Reporting Services.
  GenerateReportPDF("/Standard Reports/Process Overview", "PDF");
 }
}

K2 blackpearl Developers Reference4.7
Video Links Learn Support
No videos found for this article
K2 on YouTube
No Additional links found for this article
No self-learning content for this article
Try some scenarios...
No relevant support links available for this article