HtmlRendererCore: HTML to PDF Rendering for .NET Core

HtmlRendererCore: HTML to PDF Rendering for .NET Core

Summary

HtmlRendererCore is a partial port of the popular HtmlRenderer library, specifically designed to bring robust HTML to PDF conversion capabilities to .NET Core projects. It leverages PdfSharpCore to enable developers to easily generate PDF documents from HTML content, making it a valuable tool for various application needs.

Repository Info

Updated on October 12, 2025
View on GitHub

Tags

Click on any tag to explore related repositories

Introduction

HtmlRendererCore is an essential library for .NET Core developers needing to convert HTML content into PDF documents. As a partial port of the well-known HtmlRenderer, it extends its powerful rendering capabilities to the modern .NET Core ecosystem. This project integrates seamlessly with PdfSharpCore, providing a straightforward solution for generating high-quality PDFs directly from HTML strings.

Installation

HtmlRendererCore is available as a NuGet package, making it easy to integrate into your .NET Core projects. You can add it using the NuGet Package Manager or via the .NET CLI:

dotnet add package HtmlRendererCore.PdfSharp

This package includes the necessary components for HTML to PDF conversion using PdfSharpCore.

Examples

The library provides simple and intuitive methods for generating PDFs. Here are a couple of common use cases:

Generate PDF from HTML

To convert an HTML string into a PDF document, you can use the PdfGenerator class:

var pdf = PdfGenerator.GeneratePdf(html, PdfSharpCore.PageSize.A4);
// 'pdf' is now a PdfDocument object that can be saved or manipulated.

Generating Base64 PDF from HTML

For scenarios where you need the PDF content as a Base64 encoded string, such as for API responses or embedding, you can do the following:

var result = string.Empty;

using (var stream = new MemoryStream())
{
    var pdf = PdfGenerator.GeneratePdf(html, PdfSharpCore.PageSize.A4);

    pdf.Save(stream);

    result = Convert.ToBase64String(stream.ToArray());
}
// 'result' now contains the Base64 encoded PDF string.

Why Use It?

HtmlRendererCore fills a crucial gap for .NET Core developers by offering a reliable and efficient way to convert HTML to PDF. Its benefits include:

  • .NET Core Compatibility: Specifically designed for the modern .NET Core framework.
  • Ease of Use: Simple API for quick integration and PDF generation.
  • Leverages PdfSharpCore: Builds upon a robust and widely used PDF library.
  • Open Source: Actively maintained and open to community contributions, ensuring flexibility and transparency.
  • Versatile: Suitable for various applications, from generating reports and invoices to dynamic content archiving.

Links