Requirements
EggPdf targets netstandard2.0 through net10.0, so it works with any modern .NET project:
- .NET 6, 7, 8, 9, or 10
- .NET Framework 4.6.1+ (via netstandard2.0)
- No additional system libraries, fonts, or browser runtimes required
EggPdf ships with built-in font metrics and does not require system fonts to be installed. Custom fonts can be loaded from bytes or file paths.
Installation
Add EggPdf to your project using the .NET CLI:
dotnet add package EggPdfOr via the NuGet Package Manager in Visual Studio — search for EggPdf. You can also add it directly to your .csproj:
YourProject.csproj
<PackageReference Include="EggPdf" Version="0.3.0" />Your First PDF
The entire public API is one static method: HtmlToPdf.RenderAsync. It accepts an HTML string and returns a byte[] containing the PDF document.
Program.cs
using EggPdf;
var html = """
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
h1 { color: #2563eb; border-bottom: 2px solid #e2e8f0; padding-bottom: 8px; }
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
th, td { padding: 8px 12px; border: 1px solid #e2e8f0; }
th { background: #f8fafc; font-weight: 600; }
</style>
</head>
<body>
<h1>Invoice #1001</h1>
<table>
<thead><tr><th>Item</th><th>Qty</th><th>Price</th></tr></thead>
<tbody>
<tr><td>Widget A</td><td>3</td><td>$9.99</td></tr>
<tr><td>Widget B</td><td>1</td><td>$24.99</td></tr>
</tbody>
</table>
</body>
</html>
""";
var pdf = await HtmlToPdf.RenderAsync(html);
await File.WriteAllBytesAsync("invoice.pdf", pdf);
Console.WriteLine($"PDF written: {pdf.Length:N0} bytes");Render Options
Pass a PdfRenderOptions object to customise page size, margins, title, and headers/footers:
Program.cs
using EggPdf;
var options = new PdfRenderOptions
{
PageSize = PageSize.A4,
Margins = new PageMargins(40, 50, 40, 50), // top, right, bottom, left (px)
Title = "My Document",
Footer = "Page {page} of {pages}",
};
var pdf = await HtmlToPdf.RenderAsync(html, options);| Option | Type | Default | Description |
|---|---|---|---|
PageSize | PageSize | A4 | Page size (A4, Letter, Legal, A3, A5…) |
Margins | PageMargins | 40px all sides | Top, right, bottom, left margins in CSS pixels |
Title | string? | null | PDF document title metadata |
Footer | string? | null | Footer template. Supports {page} and {pages} |
Header | string? | null | Header template. Supports {page} and {pages} |
ASP.NET Core Integration
Inject EggPdf into your ASP.NET Core application to serve PDFs from controller actions or Razor pages.
InvoiceController.cs
using EggPdf;
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/[controller]")]
public class InvoiceController : ControllerBase
{
[HttpGet("{id}")]
public async Task<IActionResult> GetPdf(int id)
{
var html = await BuildInvoiceHtmlAsync(id);
var options = new PdfRenderOptions { Title = $"Invoice #{id}" };
var pdf = await HtmlToPdf.RenderAsync(html, options);
return File(pdf, "application/pdf", $"invoice-{id}.pdf");
}
}Minimal API (ASP.NET Core 6+)
Program.cs
using EggPdf;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapPost("/pdf", async (RenderRequest req) =>
{
var pdf = await HtmlToPdf.RenderAsync(req.Html);
return Results.File(pdf, "application/pdf");
});
app.Run();
record RenderRequest(string Html);Next Steps
- Deploy with Docker — run EggPdf as a REST API service
- Use the CLI tool — convert HTML files from the terminal or CI
- REST API reference — full endpoint and options documentation
- GitHub repository — source code, issues, and releases