🚀 Generate production-ready PDFs using Razor components and C# in minutes.
H2Pdf bridges modern .NET UI development and deterministic PDF generation. Build PDFs with familiar .razor components, strongly typed C#, and dependency injection.
- Razor components as PDF templates
- Fluent PDF document model API
- Optional HTML-to-PDF pipeline (
HtmlPdfRenderer) - ASP.NET Core dependency injection integration
- Cross-platform rendering (Windows, Linux, macOS)
Requirements: .NET 10 SDK
git clone https://github.com/evilz/H2Pdf.git
cd H2Pdf
dotnet build
cd samples/H2Pdf.Sample
dotnet runGenerated files:
sample-output.pdfinvoice-sample.pdf
Install via NuGet:
dotnet add package H2PdfOr clone from source:
git clone https://github.com/evilz/H2Pdf.git
cd H2Pdf
dotnet buildvar services = new ServiceCollection();
services.AddLogging();
services.AddH2Pdf();
var provider = services.BuildServiceProvider();
var renderer = provider.GetRequiredService<PdfRenderer>();
var parameters = new Dictionary<string, object?>
{
["Name"] = "Developer",
["Message"] = "This PDF was generated from a Razor component."
};
var document = await renderer.RenderToPdfAsync<HelloWorld>(parameters);
renderer.SaveToPdf(document, "output.pdf");See samples/ and examples/ for complete usage patterns.
Compare Playwright HTML-to-PDF with H2Pdf HTML-to-MigraDoc rendering:
dotnet build benchmarks/H2Pdf.Benchmarks
pwsh benchmarks/H2Pdf.Benchmarks/bin/Debug/net10.0/playwright.ps1 install chromium
dotnet run -c Release --project benchmarks/H2Pdf.BenchmarksEnvironment
- BenchmarkDotNet v0.14.0
- .NET 10.0.2 (RyuJIT, AVX2)
- Windows 11 x64
- GC: Concurrent Workstation
-
Playwright (Chromium)
- HTML rendered in headless Chromium
- PDF generated via
Page.PdfAsync()
-
MigraDoc (via HtmlPdfRenderer)
- HTML → MigraDoc DOM
- MigraDoc → PDF via
PdfDocumentRenderer
| Method | Mean | StdDev | Gen0 | Gen1 | Allocated |
|---|---|---|---|---|---|
| PlaywrightHtmlToPdfAsync | 9.231 ms | 0.196 ms | 15.63 | - | 188 KB |
| MigraDoc_HtmlToPdf | 4.684 ms | 0.014 ms | 312.50 | 109.38 | 2.68 MB |
These numbers are an example from one environment and one HTML payload.
Results will vary by machine, OS, runtime version, browser version, and template complexity.
- MigraDoc: ~4.68 ms
- Playwright: ~9.23 ms
| Pipeline | StdDev |
|---|---|
| MigraDoc | 0.014 ms |
| Playwright | 0.196 ms |
| Pipeline | Allocated per PDF |
|---|---|
| Playwright | ~188 KB |
| MigraDoc | ~2.68 MB |
GC activity:
- MigraDoc → Triggers Gen0 + Gen1
- Playwright → Mostly Gen0 only
Use benchmark output in your environment to compare trade-offs for your workloads.
Most PDF generation tools force teams to use low-level primitives or separate template systems. H2Pdf keeps PDF authoring in your existing .NET workflow with:
- component-driven composition
- familiar Razor syntax
- predictable, code-reviewable output
See ROADMAP.md.
We welcome PRs and ideas. Start with CONTRIBUTING.md.
High-level internals are documented in Architecture.md.
MIT — see LICENSE.