Transforming a SSRS(RDLC)Report report output to PDF format

Sksamantaray
Posted by in ASP.NET category on for Beginner level | Points: 250 | Views : 17106 red flag

Transforming the report output into PDF is an easy task now.


 Download source code for Transforming a SSRS(RDLC)Report report output to PDF format

Introduction

Sql Server Reporting service has got a wonderful option called export which in fact helps to export the report output to PDF or Excel. However unfortunately it does not allow user to generate any specific type of output like PDF or excel. So in this article we are going to see how to create only PDF file from the report output.

Objective

To transform a RDLC report output to a pdf file

Requisites

1.Create a SSRS RDLC Report
2.Create Function Which can convert Report output to PDF

Using the code

In this code we are going to create a report to show top 20 employee details.After the report is created, it needs to be converted to pdf manually.  So in the under mentioned code the export control is removed ,intentionally ,by using  ShowExportControls=false.
The getData() method of the code behind populates employee details in the report-viewer.
When one clicks on Export2PDF button the output of Report-Viewer gets converted to PDF  and the user is asked to save the file to physical location .
In this code I have marked a commented section which means the pdf can also be saved to any specified disk location provided permission is granted to the user.

 <form id="form1" runat="server">
    <div>
    
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            Text="Export2PDF" Width="106px" />
        <br />
    
        <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" ShowExportControls="false" 
            Font-Size="8pt" Height="400px" Width="100%">
            <LocalReport ReportPath="Report1.rdlc">
                <DataSources>
                    <rsweb:ReportDataSource DataSourceId="ObjectDataSource1" 
                        Name="DataSet1_Employees" />
                </DataSources>
            </LocalReport>
        </rsweb:ReportViewer>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
            SelectMethod="GetDataItem" TypeName="RDLC_webApplication1._Default">
        </asp:ObjectDataSource>
    
    </div>
    </form>                                                                                     // Code Behind                                                                                                      string connn = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!Page.IsPostBack)
            {
                getData();
            }
        }

        protected void getData()
        {

            SqlDataAdapter da = new SqlDataAdapter("select top 20 * from Employees", connn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            ReportDataSource rdc = new ReportDataSource("DataSet1_Employees", ds.Tables[0]);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rdc);
            ReportViewer1.Visible = true;
            ReportViewer1.LocalReport.Refresh();


        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Warning[] warnings;

            string[] streamids;

            string mimeType;

            string encoding;

            string extension;
            byte[] bytes = this.ReportViewer1.LocalReport.Render("PDF", null, out mimeType, out                   encoding, out extension, out streamids, out warnings);
            Response.BinaryWrite(bytes);

            //  writing to disk
            //FileStream fs = new FileStream(@"c:\ReportOutput.pdf", FileMode.Create);
            //FileStream fs = new FileStream(Server.MapPath(@"pdfs\abc.pdf"), FileMode.Create);
            //fs.Write(bytes, 0, bytes.Length);

            //fs.Close();

        } 
When user first loads the page ,the getData() method is called,which takes care of loading employee details in the report-viewer. Here you cannot see any export option since it is made to false as i mention earlier in this article. To create a pdf this , user needs to click on Export2PDF button.




After clicking on Export2PDF button , user is asked to save the file to a physical location as shown in the figure.


Conclusion

This is how a PDF can be created manually  from SSRS- RDLC Report output.

Page copy protected against web site content infringement by Copyscape

About the Author

Sksamantaray
Full Name: Sanjaya Kumar Samantaray
Member Level: Silver
Member Status: Member
Member Since: 11/6/2011 3:05:05 PM
Country: India
Thanks, Sanjay

Wipro Technology

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)