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
<PackageReference Include="Mavusi.HtmlRendererCore.PdfSharp" Version="2.1.2" />
<PackageVersion Include="Mavusi.HtmlRendererCore.PdfSharp" Version="2.1.2" />
<PackageReference Include="Mavusi.HtmlRendererCore.PdfSharp" />
paket add Mavusi.HtmlRendererCore.PdfSharp --version 2.1.2
#r "nuget: Mavusi.HtmlRendererCore.PdfSharp, 2.1.2"
#:package Mavusi.HtmlRendererCore.PdfSharp@2.1.2
#addin nuget:?package=Mavusi.HtmlRendererCore.PdfSharp&version=2.1.2
#tool nuget:?package=Mavusi.HtmlRendererCore.PdfSharp&version=2.1.2
HtmlRendererCore.PdfSharp
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
- Reuse CSS Data: Parse stylesheets once and reuse for multiple PDFs
- Optimize Images: Use appropriately sized images to reduce PDF file size
- Minimize HTML: Remove unnecessary whitespace and comments
- 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
- Built on PdfSharpCore
- Based on HtmlRenderer
- Uses Liberation Fonts (SIL Open Font License)
Support
| Product | Versions 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. |
-
net10.0
- Mavusi.PdfSharpCore (>= 2.0.0)
- Newtonsoft.Json (>= 13.0.4)
-
net8.0
- Mavusi.PdfSharpCore (>= 2.0.0)
- Newtonsoft.Json (>= 13.0.4)
-
net9.0
- Mavusi.PdfSharpCore (>= 2.0.0)
- Newtonsoft.Json (>= 13.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v2.1.2: Improved documentation with comprehensive examples and usage guide. Removed unnecessary SixLabors.ImageSharp dependency for reduced package footprint.