Dynamically Create And Download PDF From RDLC Report

This blog explains how to create and download a pdf from a rdlc report without manual work.
 
Considering you already have a RDLC reporting tool in your application, and you are able to view reports in the ReportViewer.
 
Pass the report to convert to pdf.
  1. ReportDataSource rds = new ReportDataSource();  
  2.   
  3. ds.Name = "DataSet2";  
  4.                rds.Value = dt;  
  5.                rdsAPP.LocalReport.ReportPath = Server.MapPath("Report.rdlc");  
  6.                rdsAPP.LocalReport.DataSources.Clear();  
  7.                rdsAPP.LocalReport.DataSources.Add(rds);  
  8.                string Url = ConvertReportToPDF(rdsAPP.LocalReport);  
  9.                System.Diagnostics.Process.Start(Url);
rdsAPP is report viewer in aspx file.
  1. private string ConvertReportToPDF(LocalReport rep)
    {
  2. string reportType = "PDF";  
  3.         string mimeType;  
  4.         string encoding;  
  5.   
  6.         string deviceInfo = "<DeviceInfo>" +  
  7.            "  <OutputFormat>PDF</OutputFormat>" +  
  8.            "  <PageWidth>8.27in</PageWidth>" +  
  9.            "  <PageHeight>6.0in</PageHeight>" +  
  10.            "  <MarginTop>0.2in</MarginTop>" +  
  11.            "  <MarginLeft>0.2in</MarginLeft>" +  
  12.            "  <MarginRight>0.2in</MarginRight>" +  
  13.            "  <MarginBottom>0.2in</MarginBottom>" +  
  14.            "</DeviceInfo>";  
  15.   
  16.         Warning[] warnings;  
  17.         string[] streamIds;  
  18.         string extension = string.Empty;  
  19.   
  20.         byte[] bytes = rep.Render(reportType, deviceInfo, out mimeType, out encoding, out extension, out streamIds, out warnings);  
  21.         //string localPath = System.Configuration.ConfigurationManager.AppSettings["TempFiles"].ToString();  
  22.         string localPath = AppDomain.CurrentDomain.BaseDirectory;  
  23.         string fileName = Guid.NewGuid().ToString() + ".pdf";  
  24.         localPath = localPath + fileName;  
  25.         System.IO.File.WriteAllBytes(localPath, bytes);  
  26.         return localPath;
  27. }  
The above code will create a pdf and will download it in localpath (Path should be mentioned as per requirement) with file name and the function will return the path to the main function.
  1. <PageWidth>8.27in</PageWidth>  
  2. <PageHeight>6.0in</PageHeight>  
All these parameters will help you to set the height and width of the pdf form.
 
This will automatically download the pdf file generated in the local path.
  1. System.Diagnostics.Process.Start(Url);  
Overview
  1. Pass the local report to the ConvertReportToPDF function
  2. Convert the file into bytes
  3. Generate random id using Guid function. 
  4. Get the application source path.
  5. Save the file in the given localpath.
  6. Pass the local path to Process function
  7. It will download the file. 
Thank you for reading :) 
Next Recommended Reading How to use BarCode in RDLC based Report