FastReport OpenSource Generate Pdf Without PdfSimple

Yusuf Bal
5 min readApr 5, 2022

--

Generate PDF Reports With FastReport Open Source, without PdfSimplePlugin.

FastReport provides open source report generator for .NET6/.NET Core/.NET Framework 4.x. You can use the FastReport in MVC, Web API applications. FastReport Open Source is based on the FastReport.Net project. You can find more information at https://github.com/FastReports/FastReport.Documentation

FastReport Open Source can save documents in HTML, BMP, PNG, JPEG, GIF, TIFF, EMF.

When you want to export report with PDF format, you must use FastReport.OpenSource.Export.PdfSimple plugin. You can find more information at https://github.com/FastReports/FastReport/tree/master/Extras/OpenSource/FastReport.OpenSource.Export.PdfSimple

PdfSimple plugin exports pdf files as images. For example, if you have 10 pages of an exported report, PdfSimple converts every page as an image then it merges all images in one pdf file. Therefore, with this plugin, the size of your pdf output is too large and you cannot copy the texts from the pdf output.

If you don’t want use PdfSimple, FastReport says you can use commercial version. You can find more information here: https://github.com/FastReports/FastReport/issues/86

In this article, we use itext7 for solve this problem.

First, create a new class project and add the nuget packages you see below to this project:

FastReport.Compat, FastRepor.OpenSource: We use this packages for create reports.

FastReport.OpenSource.Export.PdfSimple: We use this package for export report to pdf. We will use this package in the project to compare with the pdf file we will create with the new method.

itext7, itext7.pdfhtml: We use this projects for export html reports to pdf.

While FastReport reports are output as html, it uses the frpage div tag for pagination. When converting html pages to pdf, the first thing we need to do is to capture these divs and make them pass to the new page.

Let’s add a class called Utils and add a property named PageDivProperty to this class. This property will be used to mark the pagination divs in the html outputs of the reports.

We write a DivTagWorker to find frpage tags within the itext7 library.

In order to use this tag worker we have created, we must use DefaultTagWorkerFactory. In this factory, we will use the CustomPageTagWorker class that we wrote when there are div tags in the html elements.

Now we can catch the transitions to the new page in the html outputs created by FastReport.

Let’s create a new class called FastReport Generator. This class takes a generic T object that will use report patterns. The class’s constructor must take two parameters: designPath and designFileName:

designPath: The main folder containing the FastReport design files.
designFileName: Name of the design file of the report to be created ( frx extension )

Then we create CreateReportWithData method for return Report object:

In this project, we create a method named GenerateWithPDFSimplePlugin to compare the pdf files that we will create with the PdfSimplePlugin. This method will create a new pdf with the plugin and return the byte array of this pdf file it created.

With the methods we have written so far, we can create pdf with the libraries that FastReport gives us as open source. We will see how to convert html report outputs generated by FastReport to pdf using the itext7 library with the following methods.

We write GenerateHtml method to export html report with FastReport library.

With this method, we are able to obtain the html output of the report. Now we need to convert this html output to pdf. Let’s add a new method named GeneratePdfFromHtml.

Let’s talk about some features of this method:

  • FontProvider fontProvider = new DefaultFontProvider(true, true, true): While converting html outputs to pdf with itext7, if you want to use the default fonts you created with FastReport Designer and support utf8 characters, you should create a FontProvider like new DefaultFontProvider(true, true, true) and set it to the converter you will create.
  • converterProperties.SetTagWorkerFactory(new CustomTagWorkerFactory()): We register the worker we created to detect the pages in the html output.
  • var elements = HtmlConverter.ConvertToElements(reportHtml, converterProperties): In order to detect page transitions, we add the new page transition when we look at the html elements one by one and see the divs we marked with the worker.
  • document.SetMargins(0, 0, 0, 0): We set the margins to 0 to prevent the default margins of the itext7 library.

You can see the full code for the FastReportGenerator class below.

Let’s create a console application for testing and create two separate pdfs as follows.

In this application, we create a model with 1000 records and pass it to the report design called test.frx. The name of the file we created with the pdfsimple plugin: testWithPdfSimple.pdf. The name of the file we created with itext7: testWithoutPdfSimple.

As you can see above, the size of the file created with pdfsimple is 38 mb, while the size of the file we created with itext7 is 94kb. At the same time, we cannot copy the texts because the file you created with pdf simple consists of images. However, since the pdf we created with itext7 consists of html outputs, you can copy the texts from the pdf.

You can find project here: https://github.com/yusufbal/FastReport.OpenSource.HtmlExporter

You can use the FastReport.Open Source.Html Exporter.Core project in the repository on the github link in your asp.net web forms, asp.net mvc, asp.net core projects.

I hope this article helps you. See you in new articles.

--

--

Yusuf Bal

computer engineer. software development lead @kocsistem