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.
- ReportDataSource rds = new ReportDataSource();
-
- ds.Name = "DataSet2";
- rds.Value = dt;
- rdsAPP.LocalReport.ReportPath = Server.MapPath("Report.rdlc");
- rdsAPP.LocalReport.DataSources.Clear();
- rdsAPP.LocalReport.DataSources.Add(rds);
- string Url = ConvertReportToPDF(rdsAPP.LocalReport);
- System.Diagnostics.Process.Start(Url);
rdsAPP is report viewer in aspx file.
-
private string ConvertReportToPDF(LocalReport rep)
{
- string reportType = "PDF";
- string mimeType;
- string encoding;
-
- string deviceInfo = "<DeviceInfo>" +
- " <OutputFormat>PDF</OutputFormat>" +
- " <PageWidth>8.27in</PageWidth>" +
- " <PageHeight>6.0in</PageHeight>" +
- " <MarginTop>0.2in</MarginTop>" +
- " <MarginLeft>0.2in</MarginLeft>" +
- " <MarginRight>0.2in</MarginRight>" +
- " <MarginBottom>0.2in</MarginBottom>" +
- "</DeviceInfo>";
-
- Warning[] warnings;
- string[] streamIds;
- string extension = string.Empty;
-
- byte[] bytes = rep.Render(reportType, deviceInfo, out mimeType, out encoding, out extension, out streamIds, out warnings);
-
- string localPath = AppDomain.CurrentDomain.BaseDirectory;
- string fileName = Guid.NewGuid().ToString() + ".pdf";
- localPath = localPath + fileName;
- System.IO.File.WriteAllBytes(localPath, bytes);
- return localPath;
- }
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.
- <PageWidth>8.27in</PageWidth>
- <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.
- System.Diagnostics.Process.Start(Url);
Overview
- Pass the local report to the ConvertReportToPDF function
- Convert the file into bytes
- Generate random id using Guid function.
- Get the application source path.
- Save the file in the given localpath.
- Pass the local path to Process function
- It will download the file.
Thank you for reading :)