How to Generate Server-Side Reports in .NET Core

watch_later 7/11/2024

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.

How to Generate Server-Side Reports in .NET Core

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?

It is a technique for generating reports on the server and then streams 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, simple, yet very powerful since it easily supports all customization options available in the conversion process.

FastReport Open Source

fast report generation with minimal efforts. Supports multiple data sources and 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 examples shown, you can easily integrate these tools into your .NET Core applications to enhance their reporting capabilities.

Codingvila provides articles and blogs on web and software development for beginners as well as free Academic projects for final year students in Asp.Net, MVC, C#, Vb.Net, SQL Server, Angular Js, Android, PHP, Java, Python, Desktop Software Application and etc.

Thank you for your valuable time, to read this article, If you like this article, please share this article and post your valuable comments.

Once, you post your comment, we will review your posted comment and publish it. It may take a time around 24 business working hours.

Sometimes I not able to give detailed level explanation for your questions or comments, if you want detailed explanation, your can mansion your contact email id along with your question or you can do select given checkbox "Notify me" the time of write comment. So we can drop mail to you.

If you have any questions regarding this article/blog you can contact us on info.codingvila@gmail.com

sentiment_satisfied Emoticon