Search results

Export RDL report in Bold Reports ASP.NET Core Report Writer

The report writer is a class library that is used to export the RDL/RDLC report with popular file formats like PDF, Microsoft Word, and Microsoft Excel without previewing the report in web page. This section describes how to export the RDL report to PDF in ASP.NET Core application using the ReportWriter.

List of dependency libraries

Right-click the project or solution in the Solution Explorer tab, and choose Manage NuGet Packages. Alternatively, go to Tools > NuGet Package Manager > Manage NuGet Packages for Solution menu command and then search for BoldReports.Net.Core package, and install them in your Core application. The following table provides details about the package and their usage.

Package Purpose
BoldReports.Net.Core Creates ReportWriter controls to export the reports.

The following table provides details about the dependency packages and its usage.

Package Purpose
Syncfusion.Compression.Net.Core Exports the report to PDF, Microsoft Word, and Microsoft Excel format. It is a base library for the Syncfusion.Pdf.Net.Core , Syncfusion.DocIO.Net.Core, and Syncfusion.XlsIO.Net.Core packages.
Syncfusion.Pdf.Net.Core Exports the report to a PDF.
Syncfusion.DocIO.Net.Core Exports the report to a Word.
Syncfusion.XlsIO.Net.Core Exports the report to an Excel.
Syncfusion.OfficeChart.Net.Core It is a base library of the Syncfusion.XlsIO.Net.Core package.
Newtonsoft.Json Serializes and deserialize data for the Report Viewer. It is a mandatory package for Report Viewer, and the package version should be higher than 10.0.1 for NET Core 2.0 and others should be higher than 9.0.1.
System.Data.SqlClient This is an optional package for Report Viewer. It should be referenced in project when the RDL report renders visual data from the SQL Server or SQL Azure data source based on RDL design. The package version should be higher than 4.1.0.

Generate the PDF file

We have to use the Save method in ReportWriter to generate the PDF document along with information of the report stream, it will return the generated file as Stream. The report writer supports generating the files with formats of PDF, Word, and Excel. Here, the sample codes are explained to generate the PDF and the WriterFormat is used as PDF.

public class HomeController : Controller
   {
       // IHostingEnvironment used with sample to get the application data from wwwroot.
       private Microsoft.AspNetCore.Hosting.IHostingEnvironment _hostingEnvironment;

       // IHostingEnvironment initialized with controller to get the data from application data folder.
       public HomeController(Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment)
       {
           _hostingEnvironment = hostingEnvironment;
       }

       [HttpPost]
       public IActionResult Pdf()
       {
           string basePath = _hostingEnvironment.WebRootPath;
           // Here, we have loaded the sample report report from application the folder wwwroot.
           // Invoice.rdl should be there in wwwroot application folder.
           FileStream inputStream = new FileStream(basePath + @"\Invoice.rdl", FileMode.Open, FileAccess.Read);
           BoldReports.Writer.ReportWriter writer = new BoldReports.Writer.ReportWriter(inputStream);

           // Steps to generate PDF report using Report Writer.

           MemoryStream memoryStream = new MemoryStream();
           writer.Save(memoryStream, BoldReports.Writer.WriterFormat.PDF);

           // Download the generated from client.
           memoryStream.Position = 0;
           FileStreamResult fileStreamResult = new FileStreamResult(memoryStream, "application/pdf");
           fileStreamResult.FileDownloadName = "Invoice.pdf";
           return fileStreamResult;
       }
}

You cannot load the application report with path information in the ASP.NET Core. So, you should load the report as stream like an example provided above in the Pdf method.

This is an example home page to invoke the Web API from client for the above sample codes:

    @{Html.BeginForm("Pdf", "Home", FormMethod.Post);
    {
        <div>
            <input type="submit" value="Generate PDF" style="width: 150px;" />
        </div>
    }
        Html.EndForm();
    }

Parameters for report

SetParameters methods can be used with report writer to generate the file format report based on parameters. The following sample code explains passing the parameter based on the information received from the client.

public class HomeController : Controller
    {
        // IHostingEnvironment used with sample to get the application data from wwwroot.
        private Microsoft.AspNetCore.Hosting.IHostingEnvironment _hostingEnvironment;

        // IHostingEnvironment initialized with controller to get the data from application data folder.
        public HomeController(Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }

        [HttpPost]
        public IActionResult Pdf()
        {
            string basePath = _hostingEnvironment.WebRootPath;
            // Here, we have loaded the sample report report from application the folder wwwroot.
            // Invoice.rdl should be there in wwwroot application folder.
            FileStream inputStream = new FileStream(basePath + @"\Invoice.rdl", FileMode.Open, FileAccess.Read);
            BoldReports.Writer.ReportWriter writer = new BoldReports.Writer.ReportWriter(inputStream);

            // Assigning the report parameter based on selected value from user.
            string invoiceID = this.HttpContext.Request.Form["invoiceId"].ToString();

            List<BoldReports.Web.ReportParameter> parameters = new List<BoldReports.Web.ReportParameter>();
            BoldReports.Web.ReportParameter param = new BoldReports.Web.ReportParameter();
            param.Name = "InvoiceID";
            param.Values = new List<string>() { invoiceID };
            parameters.Add(param);
            writer.SetParameters(parameters);

            // Steps to generate PDF report using Report Writer.
            MemoryStream memoryStream = new MemoryStream();
            writer.Save(memoryStream, BoldReports.Writer.ReportWriter.WriterFormat.PDF);

            // Download the generated from client.
            memoryStream.Position = 0;
            FileStreamResult fileStreamResult = new FileStreamResult(memoryStream, "application/pdf");
            fileStreamResult.FileDownloadName = "Invoice.pdf";
            return fileStreamResult;
        }
}

This is an example page to invoke the Web API from client for the above sample codes.

    @{Html.BeginForm("Pdf", "Home", FormMethod.Post);
    {
        <div>
            <input name="invoiceId" value="10255" style="width: 150px;" />
            <input type="submit" value="Generate PDF" style="width: 150px;" />
        </div>
    }
        Html.EndForm();
    }
    ```

> The invoice sample report can be obtained from the Bold Reporting Tools ASP.NET Core sample browser installed location `(wwwroot\reports\invoice.rdl)`.