How to Generate Server Side Reports in .NET Core

Introduction

Server-side reporting is one of the common requirements in most web apps. This will enable the end-users to create detailed dynamic documents based on user input and data available from the application. A number of free and strong packages exist in.NET Core that make this easier. These libraries save money while providing strong features and flexibility in order to guarantee a good fit for varied reporting needs.

I have a client who wants a solution for several types of server-side reports to be developed in one of my current projects. The goal is to find and integrate free packages that would easily solve report generation but with .NET Core. Such a solution shall also be easy to work with, maintain, and extend. It covers some of the best free packages for server-side reporting in .NET Core, gives a full example of how to get started, and shows how to integrate these tools effectively into your projects.

Table of Contents

  • What is Server-Side Reporting?
  • Top Free Packages for .NET Core
    • DinkToPdf
    • FastReport Open Source
    • Report.NET
  • Detailed Examples
    • Generating PDF Reports with DinkToPdf
    • Creating Reports with FastReport Open Source
    • Using Report.NET for Custom Reports
  • Conclusion

What is Server-Side Reporting?

It is a technique for generating reports on the server and then streaming the result to the client to use in a viewer or save to disk. This is an important technique to use when building applications that need to process data in complex ways for reporting and where report generation needs to be secure.

Best free .NET core packages
 

DinkToPdf

This is a wrapper class for the Web-kit rendering engine utilized in converting HTML documents to PDF documents. It is light in weight, and simple, yet very powerful since it easily supports all customization options available in the conversion process.

FastReport Open Source

fast report generation with minimal effort. Supports multiple data sources and a rich set of features.

Report.NET

Report.NET This is a free and open-source library for generating PDF reports using .NET Core. It is lightweight and provides all of the basic functionalities to generate custom reports.

Detailed Examples
 

Generating PDF Reports with DinkToPdf

Step 1. Installing the Package

Install the DinkToPdf package via NuGet.

dotnet add package DinkToPdf
dotnet add package DinkToPdf.Native.Linux64
dotnet add package DinkToPdf.Native.Windows

Step 2. Configure DinkToPdf

Configure DinkToPdf in the Startup.cs file.

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
}

Step 3. Create a PDF Report.

Create a controller action to create the PDF.

using DinkToPdf;
using DinkToPdf.Contracts;
using Microsoft.AspNetCore.Mvc;
public class ReportController : Controller
{
    private readonly IConverter _converter;
 
    public ReportController(IConverter converter)
    {
        _converter = converter;
    }
    public IActionResult GeneratePdfReport()
    {
        var doc = new HtmlToPdfDocument()
        {
            GlobalSettings = {
                PaperSize = PaperKind.A4,
                Orientation = Orientation.Portrait
            },
            Objects = {
                new ObjectSettings() {
                    PagesCount = true,
                    HtmlContent = "<h1>Hello Codingvila</h1>",
                    WebSettings = { DefaultEncoding = "utf-8" }
                }
            }
        };
        var pdf = _converter.Convert(doc);
        return File(pdf, "application/pdf", "report.pdf");
    }
}

Reports Using FastReport Open Source

Step 1. Install the Package

Install the FastReport Open Source package via NuGet.

dotnet add package FastReport.OpenSource
dotnet add package FastReport.OpenSource.Data

Step 2. Create a Report

Create a simple report.

using FastReport;
using FastReport.Export.PdfSimple;
using Microsoft.AspNetCore.Mvc;
public class FastReportController : Controller
{
    public IActionResult GenerateReport()
    {
        var report = new Report();
        report.Load("path/to/your/report.frx");
        using var stream = new MemoryStream();
        report.Prepare();
        report.Export(new PDFSimpleExport(), stream);
        stream.Position = 0;
        return File(stream.ToArray(), "application/pdf", "report.pdf");
    }
}

Using Report.NET to generate customized reports

Step 1. Installing the Package

Use NuGet to install the Report.NET package.

dotnet add package Report.NET

Step 2. Creating a Custom Report

Generate a custom report.

using Report.NET;
using Microsoft.AspNetCore.Mvc; 
public class ReportNetController : Controller
{
    public IActionResult GenerateCustomReport()
    {
        var report = new Report();
        report.AddTitle("Sample Report");
        report.AddParagraph("This is a sample report generated by codingvila using Report.NET.");
        var pdf = report.ExportToPDF();
        return File(pdf, "application/pdf", "custom-report.pdf");
    }
}

Conclusion

It is through these free packages that generating server-side reports with .NET Core becomes easy and efficient. Be it basic PDF documents or more complex reporting using multi-page reports, DinkToPdf, FastReport Open Source, and Report.NET are robust solutions. With the help of the examples shown, you can easily integrate these tools into your .NET Core applications to enhance their reporting capabilities.


Similar Articles
Codingvila
Codingvila is an educational website, developed to help tech specialists/beginners.