PDF Export in FastReport Open Source using ASP .Net Core MVC as an example


New export for FastReport Open Source - PDF export - is something that was really lacking. This is one of the most popular electronic document management formats. We have already discussed how to export from a console .Net Core application. However, for many users, an example of PDF export from an MVC application would be much more useful.

Let's create a test ASP .Net Core MVC project. Firstly, install the necessary packages in the NuGet Package Manager:

You need to install the packages:
·         FastReport.OpenSource;
·         FastReport.OpenSource.Web;
·         FastReport.OpenSource.Export.PdfSimple.
After that, in the startup.cs file, add a line to the Configure method:
app.UseFastReport();
Now let’s turn to programming. Open the HomeController.cs file. Add libraries to using:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

In the HomeController class, change the Index method:

public IActionResult Index()
        {
            var webReport = GetReport();
            ViewBag.WebReport = webReport;
            return View();
        }
Here we created a report in the GetReport method, and assigned it to the WebReport property of the ViewBag object.
Add the GetReport () method:

public HomeController(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }

        private readonly IHostingEnvironment _hostingEnvironment;
        public WebReport GetReport()
        {
            string webRootPath = _hostingEnvironment.WebRootPath; // Get the path to the wwwroot folder
            WebReport webReport = new WebReport(); // Create a Web Report Object
            webReport.Report.Load(webRootPath + "/reports/Master-Detail.frx"); // Load the report into the WebReport object
            var dataSet = new DataSet(); // Create a data source
            dataSet.ReadXml(webRootPath + "/reports/nwind.xml"); // Open the xml database
            webReport.Report.RegisterData(dataSet, "NorthWind"); // Register the data source in the report
            return webReport;
        }
We pass the variable hostingEnvironment to the class constructor.
The GetReport method returns a web report object. In the beginning, we get the path to the wwwroot folder where the report template and the database for it will be located.
Next, we create a web report object, load the report template into it. Create a data source and register it in a report.
Let's now find a view for this method - in the Views-> Home folder of the Index.cshtml file. Change it:
@{
    ViewData["Title"] = "Home Page";
}
@await ViewBag.WebReport.Render()
Thus, we have implemented the output of the report on the web page. But our goal is to export the report to PDF.
To do this, we will add another HomeController method:

        public IActionResult Pdf()
        {
            var webReport = GetReport();
            webReport.Report.Prepare();

            using (MemoryStream ms = new MemoryStream())
            {
                PDFSimpleExport pdfExport = new PDFSimpleExport();
                pdfExport.Export(webReport.Report, ms);
                ms.Flush();
                return File(ms.ToArray(), "application/pdf", Path.GetFileNameWithoutExtension("Master-Detail") + ".pdf");
            }    
        }
Here we also used the GetReport () method to get a report. Next, we prepare it for display by the Prepare function. In order not to create a physical export file, we use the stream. We created an instance of the PDF export class and exported the report to a stream, and then returned the file in PDF format.
To use this method, you need to refine the Index view as follows:

@{
    ViewData["Title"] = "Home Page";
}
<a class="btn btn-danger" asp-controller="Home" asp-action="Pdf" ">Download PDF</a>
@await ViewBag.WebReport.Render()
We added a button that calls the PDF method in the Home controller. Let's run our web application. First we will see the web report:
Click the Download PDF button:
The file is automatically loaded by the browser. Let's open it:

The export quality is at enough high level, but since this is a simplified export, the pdf file settings are completely unavailable to us. Perhaps in the future, PDF export will be included in the Save menu of the web report object.

Comments

Post a Comment

Popular posts

FastReport Designer Community Edition

How to use FastReport Open Source in ASP.NET Core Web API application