Click here to Skip to main content
15,896,063 members
Articles / Web Development / ASP.NET

Create data driven PDF on the fly by using SQL server reporting service (SSRS)

Rate me:
Please Sign up or sign in to vote.
4.68/5 (13 votes)
17 Jun 20074 min read 232.8K   3.8K   103  
A free, mean and lean way to create data driven PDF on the fly by using SQL server reporting service (SSRS)
using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using System.Net;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

using System.Web.Services.Protocols;
using Rse2005 = ReportExecution2005;


public partial class RsPdf : System.Web.UI.Page
{
    private string RptMemo="";

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    //
    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        //we only care about first 50 characters
        string tmpStr = txtRptMemo.Text.Trim();
        
        if(""==tmpStr)
        {
            tmpStr = "Your input from web browser";
        }
        else if (tmpStr.Length > 50)
        {
            tmpStr = tmpStr.Substring(0, 50);

        }
        else
        {
            //do nothing
        }
        
        RptMemo = tmpStr;
        RenderPdf();

    }

    //Call Reporting service to render PDF
    private void RenderPdf()
    {

        // Prepare Render arguments
        string historyID = null;
        string deviceInfo = null;
        string format = "PDF";
        Byte[] results;
        string encoding = String.Empty;
        string mimeType = String.Empty;
        string extension = String.Empty;
        Rse2005.Warning[] warnings = null;
        string[] streamIDs = null;


        Rse2005.ReportExecutionService rsExec = new Rse2005.ReportExecutionService();
        rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;

        //rsExec.Credentials = new NetworkCredential(username, password, domain);

        Rse2005.ExecutionInfo ei = rsExec.LoadReport("/PdfReport/pdf01", historyID);
        Rse2005.ParameterValue[] rptParameters = new Rse2005.ParameterValue[1];

        rptParameters[0] = new Rse2005.ParameterValue();
        rptParameters[0].Name = "ReportMemo";
        rptParameters[0].Value = RptMemo;

        //render the PDF
        rsExec.SetExecutionParameters(rptParameters, "en-us");
        results = rsExec.Render(format, deviceInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);

 
        Response.AddHeader("content-disposition", "attachment; filename=pdf01.pdf");
        Response.OutputStream.Write(results, 0, results.Length);

        //This is very important if you want to directly download from stream instead of file
        Response.End();

    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Canada Canada
Yet another jack-of-all-trades programmer?


Comments and Discussions