Report export in FastReport.OpenSource
FastReport.OpenSource has gained a lot of interest among many developers. This is a great report generator with a long history. The open source version is FastReport.Core, which appeared at the beginning of 2018, but with some restrictions. Namely - curtailed exports. Thus, only the following formats are available to us:
HTML, BMP, PNG, JPEG, GIF, TIFF, EMF.
Of course, this is very little. The WebReport object displays the report in html format, so it was left.
It is noteworthy that in the WebReport object, we can only save the report to the fpx preview format.
Therefore, you will have to export the report from the application code. Let's have a look at how it will look like by example.
I will describe in details the whole process of creating a demo application so that you can repeat if you wish.
Create an ASP .Net Core 2.0 project. Next, we add packages from the NuGet repository: FastReport.OpenSource and FastReport.OpenSource.Web.
Now you need to add the use of the FastReport libraries to the Startup.cs file
Let's use the Index view. Change it like this:
We will display the report in a picture format, as well as a link to download the report in HTML format.
Initially, we will have a download button that initiates the formation of the report file html. Then comes the image. But the file for it will be generated on the fly from the GetImage method in the controller.
Let's go to the HomeController.cs controller. We will need these libraries:
To set the correct file paths on the server, we use the IHostingEnvironment interface. To do this, we pass the object of type IHostingEnvironment to the controller's constructor.
Index method is left unchanged:
Add a new method to get the report as an image. So we will export to image, for example, jpeg format:
The second method is to save the export report in html format. Roughly speaking,this methods is pretty much the same as the previous one.
In this method we got one html file. And this means that there will be no pictures in it. To save the html file with images, you need to save the files in a loop. An example of such an export can be found in the FastReport Open Source documentation:
Let's run our application:
The image contains all report pages, because we set the SeparateFiles property = false. Otherwise, you would have to display several files.
Press the button Save report in HTML:
And the file is automatically loaded by the browser.
That's all. As you can see, the implementation of export in code in FastReport Open Source has no difference from FastReport.Core.