Mavusi.HtmlRendererCore.PdfSharp 2.1.2

dotnet add package Mavusi.HtmlRendererCore.PdfSharp --version 2.1.2
                    
NuGet\Install-Package Mavusi.HtmlRendererCore.PdfSharp -Version 2.1.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mavusi.HtmlRendererCore.PdfSharp" Version="2.1.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mavusi.HtmlRendererCore.PdfSharp" Version="2.1.2" />
                    
Directory.Packages.props
<PackageReference Include="Mavusi.HtmlRendererCore.PdfSharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Mavusi.HtmlRendererCore.PdfSharp --version 2.1.2
                    
#r "nuget: Mavusi.HtmlRendererCore.PdfSharp, 2.1.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Mavusi.HtmlRendererCore.PdfSharp@2.1.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Mavusi.HtmlRendererCore.PdfSharp&version=2.1.2
                    
Install as a Cake Addin
#tool nuget:?package=Mavusi.HtmlRendererCore.PdfSharp&version=2.1.2
                    
Install as a Cake Tool

HtmlRendererCore.PdfSharp

NuGet Version

HtmlRendererCore.PdfSharp is a .NET library for converting HTML to PDF documents. Built on top of PdfSharpCore, it provides a simple API for generating PDFs from HTML content.

Features

  • 🚀 Cross-Platform: Works on Windows, Linux, and macOS (.NET 8, 9, 10)
  • 🐳 Docker & Cloud Ready: Bundled Liberation Fonts - no system fonts required
  • 💼 Environment Independent: Perfect for Docker containers, Azure Functions, AWS Lambda
  • 🎨 CSS Support: Renders HTML with CSS styling
  • 🖼️ Image Support: Handles embedded and external images
  • 📦 Lightweight: Minimal dependencies, no ImageSharp required
  • Zero Configuration: Automatic font mapping to bundled open-source fonts

Installation

Install via NuGet Package Manager:

dotnet add package Mavusi.HtmlRendererCore.PdfSharp

Or via Package Manager Console:

Install-Package Mavusi.HtmlRendererCore.PdfSharp

Quick Start

Basic HTML to PDF Conversion

using HtmlRendererCore.PdfSharp;
using PdfSharpCore;

var html = @"
    <html>
        <body>
            <h1>Hello World!</h1>
            <p>This is a PDF generated from HTML.</p>
        </body>
    </html>
";

var pdf = PdfGenerator.GeneratePdf(html, PageSize.A4);
pdf.Save("output.pdf");

HTML to PDF with Custom Margins

var html = @"
    <html>
        <head>
            <style>
                body { font-family: Arial; }
                h1 { color: #333; }
                p { margin: 10px 0; }
            </style>
        </head>
        <body>
            <h1>Professional Document</h1>
            <p>Generated with custom margins.</p>
        </body>
    </html>
";

// 40px margins on all sides
var pdf = PdfGenerator.GeneratePdf(html, PageSize.A4, margin: 40);
pdf.Save("document.pdf");

Advanced Configuration

using HtmlRendererCore.PdfSharp;
using PdfSharpCore;

var html = "<html><body><h1>Landscape Document</h1></body></html>";

var config = new PdfGenerateConfig
{
    PageSize = PageSize.A4,
    PageOrientation = PageOrientation.Landscape,
    MarginTop = 20,
    MarginBottom = 20,
    MarginLeft = 30,
    MarginRight = 30
};

var pdf = PdfGenerator.GeneratePdf(html, config);
pdf.Save("landscape.pdf");

Generate Base64 PDF String

using System.IO;

var html = "<html><body><p>PDF as Base64</p></body></html>";

string base64Pdf;
using (var stream = new MemoryStream())
{
    var pdf = PdfGenerator.GeneratePdf(html, PageSize.A4);
    pdf.Save(stream);
    base64Pdf = Convert.ToBase64String(stream.ToArray());
}

// Use base64Pdf for email attachments, web APIs, etc.

Adding Pages to Existing PDF

using PdfSharpCore.Pdf;

var document = new PdfDocument();

// Add first page
var html1 = "<html><body><h1>Page 1</h1></body></html>";
PdfGenerator.AddPdfPages(document, html1, PageSize.A4);

// Add second page
var html2 = "<html><body><h1>Page 2</h1></body></html>";
PdfGenerator.AddPdfPages(document, html2, PageSize.A4);

document.Save("multi-page.pdf");

HTML with Images

var html = @"
    <html>
        <body>
            <h1>Document with Image</h1>
            <img src='https://example.com/logo.png' width='200' />
            <p>Image loaded from URL</p>
        </body>
    </html>
";

var pdf = PdfGenerator.GeneratePdf(html, PageSize.A4);
pdf.Save("with-image.pdf");

Custom Font Mapping

// Map custom fonts to bundled Liberation fonts
PdfGenerator.AddFontFamilyMapping("MyCustomFont", "Liberation Sans");
PdfGenerator.AddFontFamilyMapping("SpecialFont", "Liberation Serif");

var html = @"
    <html>
        <head>
            <style>
                .custom { font-family: 'MyCustomFont'; }
            </style>
        </head>
        <body>
            <p class='custom'>This uses the mapped font.</p>
        </body>
    </html>
";

var pdf = PdfGenerator.GeneratePdf(html, PageSize.A4);
pdf.Save("custom-fonts.pdf");

Font Support

HtmlRendererCore.PdfSharp includes bundled Liberation Fonts for reliable cross-platform PDF generation:

  • Liberation Sans - Maps to: Arial, Helvetica, sans-serif
  • Liberation Serif - Maps to: Times New Roman, Times, serif
  • Liberation Mono - Maps to: Courier New, Courier, monospace

Why Bundled Fonts?

  • Works in Docker - No need to install system fonts
  • Works in Cloud - Azure Functions, AWS Lambda, Google Cloud Run
  • Consistent Output - Same fonts across all environments
  • Zero Configuration - Automatic fallback to bundled fonts

Advanced Usage

Custom CSS Stylesheet

var cssData = PdfGenerator.ParseStyleSheet(@"
    body { 
        font-family: 'Liberation Sans'; 
        font-size: 12pt;
        line-height: 1.5;
    }
    h1 { 
        color: #2c3e50;
        border-bottom: 2px solid #3498db;
        padding-bottom: 10px;
    }
");

var html = "<html><body><h1>Styled Header</h1><p>Paragraph text</p></body></html>";
var pdf = PdfGenerator.GeneratePdf(html, PageSize.A4, cssData: cssData);
pdf.Save("styled.pdf");

Custom Image Loading

var pdf = PdfGenerator.GeneratePdf(
    html: html,
    pageSize: PageSize.A4,
    imageLoad: (sender, e) =>
    {
        // Custom image loading logic
        // e.g., load from database, apply transformations, etc.
    }
);

System Requirements

  • .NET 8.0, 9.0, or 10.0
  • Works on Windows, Linux, and macOS
  • No additional system dependencies required

Common Use Cases

  • 📄 Generating invoices and receipts
  • 📊 Creating reports from HTML templates
  • 📧 Email attachments (using Base64 conversion)
  • 🎫 Ticket generation
  • 📋 Contract and document generation
  • 🏷️ Label and badge printing

Performance Tips

  1. Reuse CSS Data: Parse stylesheets once and reuse for multiple PDFs
  2. Optimize Images: Use appropriately sized images to reduce PDF file size
  3. Minimize HTML: Remove unnecessary whitespace and comments
  4. Async Operations: Use async/await when generating multiple PDFs

Contributing

Contributions are welcome! Please feel free to submit a Pull Request to the main repository at https://github.com/mavusi/HtmlRendererCore.

License

This library is released under the MIT License. See the LICENSE file for details.

Acknowledgments

Support

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.2 131 4/16/2026
2.1.1 95 4/16/2026
2.1.0 93 4/16/2026
2.0.0 102 4/15/2026
1.0.8 768 8/18/2025
1.0.7 357 8/18/2025
1.0.6 357 8/18/2025
1.0.5 364 8/18/2025

v2.1.2: Improved documentation with comprehensive examples and usage guide. Removed unnecessary SixLabors.ImageSharp dependency for reduced package footprint.