How to download a report in PDF format with FastReport Core
In this article we will continue covering the topic "FastReport Core + .Net Core". In the previous article we have illustrated how to display a report by exporting it to the HTML format. Now I will show how to organize downloading of a report from a browser to a local computer, using a button. We will use the export to PDF (you can use any other formats) in the function of the final file. This time it will be the SQLite database.
First, create an ASP.NET Core Web Application project and add the following libraries:
FastReport Core, Microsoft.EntitiyFrameworkCore.Sqlite, Microsoft.EntitiyFrameworkCore.Sqlite.Design. As a result, you will have the following installed packages:
Let us work with the data in advance. We need the SQLite database - fastreport.db. It can be taken from the demonstration project "C:\ Program Files (x86)\FastReports\FastReport.Net\Demos\Core\FastReportCore.MVC". Add the database directly to the root of the project. Also, we add a report template "Simple List.frx" from the folder "C:\Program Files (x86)\FastReports\FastReport.Net\Demos\Reports" to the project.
Now, in the Models folder, add a new class. We call it ApplicationDbContext.cs. As you might have understood from the title - this is the data context. Here is its content:
In this class, after connecting to the database, we get a set of data and convert it to a DataTable, which is needed for FastReport to generate the report. The model of the Employees table, that will be used in the report, is represented by a separate class.
Now go to edit HomeController.cs.
The Index method simply displays the report in HTML format, whereas the PDF method generates a PDF file for downloading. Both methods use the data context for reports.
Now change the presentation of Index.cshtml:
We have first added a PDF download button. Then, we displayed the report.
Run our application:
The article has intoduced a model of how to organize a downloading process of a report from a browser to a local computer, using a button. We just press the "Download PDF" button and download the report file in PDF format. The procedure is as simple as displaying a report.