Sunday, December 28, 2008

How to generate or render .pdf document from local report (rdlc) with parameters ?

Here is the code to render the rdlc (local report) as pdf document in asp.net web application.

public void GeneratePDFFromLocalReport()
{
//get your report datasource from the database
IDataReader iReader = GetReportDataSourceReader();
LocalReport report = new LocalReport();
report.ReportPath =
HttpContext.Current.Server.MapPath("~/reports/rdlc/MyReport.rdlc");
report.DataSources.Add(new ReportDataSource("DataSource1", iReader));
ReportParameter[] parameters = new ReportParameter[3];
parameters[0] = new ReportParameter("Filter1", "Filter1's value"));
parameters[1] = new ReportParameter("Filter2", "Filter2's value");
parameters[2] = new ReportParameter("Footer", "Footer's value");
report.SetParameters(parameters);
//
//code to render report as pdf document
string encoding = String.Empty;
string mimeType = String.Empty;
string extension = String.Empty;
Warning[] warnings = null;
string[] streamids = null;
//
byte[] byteArray = report.Render("PDF", null,
out mimeType, out encoding, out extension, out streamids, out warnings);
//
HttpContext.Current.Response.ContentType = "Application/pdf";
HttpContext.Current.Response.AddHeader("Content-Disposition",
"attachment; filename=MyPDF.pdf");
HttpContext.Current.Response.AddHeader("Content-Length",
byteArray.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.BinaryWrite(byteArray);
HttpContext.Current.Response.End();
}


-::HTH::-

1 comment:

Ricardo said...

Very good!!!
Tks man!
www.ricardokanazawa.com.br