How to Create a PDF from a RDLC Report in C#
By FoxLearn 12/7/2024 4:06:24 AM 575
In this article, we’ll walk through an example of how to use the LocalReport
class to generate a PDF invoice report programmatically. We’ll also explore how to pass dynamic data to the report and save it as a PDF file.
What Is LocalReport?
The LocalReport
class is used for generating reports in .NET applications. It works by rendering reports from .rdlc
files (Report Definition Language files), which define the layout and structure of the report. The reports can include data from a variety of sources, such as databases, datasets, or custom objects.
C# PDF to Report: Creating and Saving PDF Reports
The report will include dynamic information such as invoice details, company information, buyer details, and more. This information will be passed through ReportParameter
objects and embedded into the report.
// Render your report as a PDF, then save the byte array as a PDF file on disk. public class ReportGenerator { public static Task Pdf(InvoiceInfo obj, string filePath) { return Task.Run(() => { // Initialize the report LocalReport report = new LocalReport() { EnableExternalImages = true }; // Set the path to the .rdlc report template report.ReportPath = System.Web.Hosting.HostingEnvironment.MapPath($"~/bin/Reports/{obj.TemplateFileName}"); // Create ReportParameters for dynamic data ReportParameter pInvoiceNumber = new ReportParameter("pInvoiceNumber", obj.InvoiceNumber); ReportParameter pCompanyName = new ReportParameter("pCompanyName", obj.CompanyName); ReportParameter pCompanyAddress = new ReportParameter("pCompanyAddress", obj.CompanyAddress); ReportParameter pCompanyPhone = new ReportParameter("pCompanyPhone", obj.CompanyPhone); ReportParameter pCompanyFax = new ReportParameter("pCompanyFax", obj.CompanyFax); ReportParameter pCompanyTaxId = new ReportParameter("pCompanyTaxId", obj.CompanyTaxId); ReportParameter pBuyerName = new ReportParameter("pBuyerName", obj.BuyerName); ReportParameter pBuyerAddress = new ReportParameter("pBuyerAddress", obj.BuyerAddress); ReportParameter pBuyerTaxId = new ReportParameter("pBuyerTaxId", obj.BuyerTaxId); ReportParameter pPaymentMethod = new ReportParameter("pPaymentMethod", obj.PaymentMethod); ReportParameter pInvoiceDate = new ReportParameter("pInvoiceDate", obj.InvoiceDate); // Set parameters to the report report.SetParameters(new ReportParameter[] { pInvoiceNumber, pCompanyName, pCompanyAddress, pCompanyPhone, pCompanyFax, pCompanyTaxId, pBuyerName, pBuyerAddress, pBuyerTaxId, pPaymentMethod, pInvoiceDate }); // Add data source to the report ReportDataSource datasource = new ReportDataSource() { Name = "DataSource", Value = obj.InvoiceDetails }; report.DataSources.Add(datasource); // Refresh the report to apply all changes report.Refresh(); // Set device information for PDF output string deviceInfo = "<DeviceInfo><OutputFormat>PDF</OutputFormat><PageWidth>8.27in</PageWidth><PageHeight>11.69in</PageHeight><MarginTop>0.25in</MarginTop><MarginLeft>0.4in</MarginLeft><MarginRight>0in</MarginRight><MarginBottom>0.25in</MarginBottom></DeviceInfo>"; // Variables to hold rendering results string mimeType; string encoding; string fileNameExtension; Warning[] warnings; string[] streams; // Render the report as a PDF file byte[] renderedBytes = report.Render("PDF", deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings); // Save the rendered PDF to the specified file path using (FileStream fs = new FileStream(filePath, FileMode.Create)) { fs.Write(renderedBytes, 0, renderedBytes.Length); } }); } }
We create a LocalReport
instance and set the EnableExternalImages
property to true
, allowing external image sources (such as logos) to be embedded in the report. The report file is loaded using report.ReportPath
, pointing to the .rdlc
file.
Next, We use ReportParameter
objects to pass dynamic data to the report, such as invoice number, company information, and buyer details. These parameters are then added to the report using report.SetParameters
.
We create a ReportDataSource
object to supply the data (e.g., invoice details) to the report. This data source is added to the report using report.DataSources.Add
.
Finally, We define device-specific settings using the deviceInfo
string, specifying the desired PDF format, page size, and margins. The report.Render
method is called to render the report in PDF format, and the result is stored in a byte array (renderedBytes
).