How to Create a PDF from a RDLC Report in C#

By FoxLearn 8/1/2024 2:52:36 AM   372
To save an RDLC (Report Definition Language Client-side) report to a PDF file in C#, you can follow step.

How you can generate and save an RDLC report as a PDF file in C#

This is easy to do, you can 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(() =>
        {
            LocalReport report = new LocalReport() { EnableExternalImages = true };
            report.ReportPath = System.Web.Hosting.HostingEnvironment.MapPath($"~/bin/Reports/{obj.TemplateFileName}");            
            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);
            report.SetParameters(new ReportParameter[] { pInvoiceNumber, pCompanyName, pCompanyAddress, pCompanyPhone, pCompanyFax, pCompanyTaxId, pBuyerName,  pBuyerAddress, pBuyerTaxId, pPaymentMethod, pInvoiceDate });
            ReportDataSource datasource = new ReportDataSource() { Name = "DataSource", Value = obj.InvoiceDetails };
            report.DataSources.Add(datasource);
            report.Refresh();
            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>";
            string mimeType;
            string encoding;
            string fileNameExtension;
            Warning[] warnings;
            string[] streams;
            byte[] renderedBytes = report.Render("PDF", deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
            using (FileStream fs = new FileStream(filePath, FileMode.Create))
            {
                fs.Write(renderedBytes, 0, renderedBytes.Length);
            }
        });
    }
}

We'll create a task to handle creating and saving your rdlc report to a pdf file. You can create a task instance in many different ways.

The most common approach is to call the static Run method. The Run method provides a simple way to start a task using default values ​​and does not require additional parameters.