Nast.Html2Pdf
1.0.1
dotnet add package Nast.Html2Pdf --version 1.0.1
NuGet\Install-Package Nast.Html2Pdf -Version 1.0.1
<PackageReference Include="Nast.Html2Pdf" Version="1.0.1" />
<PackageVersion Include="Nast.Html2Pdf" Version="1.0.1" />
<PackageReference Include="Nast.Html2Pdf" />
paket add Nast.Html2Pdf --version 1.0.1
#r "nuget: Nast.Html2Pdf, 1.0.1"
#:package Nast.Html2Pdf@1.0.1
#addin nuget:?package=Nast.Html2Pdf&version=1.0.1
#tool nuget:?package=Nast.Html2Pdf&version=1.0.1
Nast.Html2Pdf
Nast.Html2Pdf is a secure-by-default HTML-to-PDF library that targets net8.0 and is intended for .NET 8+ consuming applications. It renders RazorLight templates to HTML and uses PuppeteerSharp/Chromium for PDF generation.
Runtime Support
- Package target:
net8.0 - Supported consumer runtimes: .NET 8+
- Platforms: Windows, Linux, macOS
- Browser engine: Chromium through PuppeteerSharp
Installation
dotnet add package Nast.Html2Pdf
<PackageReference Include="Nast.Html2Pdf" Version="1.0.1" />
Secure Defaults
The library starts from a conservative Chromium baseline.
AllowInsecureCertificates = falseDisableSandbox = false- no permissive
--disable-web-securitydefault - invalid template, HTML, file path, URL, and PDF option inputs fail before expensive browser work
Only opt into weaker settings when your environment truly requires them.
Supported Entry Points
Primary service API via IHtml2PdfService:
GeneratePdfAsync(template, model, pdfOptions, htmlOptions)for Razor templates plus model dataGeneratePdfFromFileAsync(templatePath, model, pdfOptions, htmlOptions)for template filesGeneratePdfFromHtmlAsync(html, pdfOptions)for already-rendered HTMLGeneratePdfFromUrlAsync(url, pdfOptions)for reachablehttporhttpsURLs
Additional convenience entry points:
Html2PdfFactory.Create(...)andHtml2PdfFactory.CreateDefault()for non-DI scenariosHtml2PdfServiceExtensionshelpers to write PDFs directly to files or streams
DI Usage
using Microsoft.Extensions.DependencyInjection;
using Nast.Html2Pdf.Abstractions;
using Nast.Html2Pdf.Extensions;
var services = new ServiceCollection();
services.AddLogging();
services.AddHtml2Pdf(options =>
{
options.MinInstances = 1;
options.MaxInstances = 3;
options.AcquireTimeoutSeconds = 30;
});
using var provider = services.BuildServiceProvider();
var html2Pdf = provider.GetRequiredService<IHtml2PdfService>();
AddHtml2Pdf() registers:
IBrowserPoolIHtmlGeneratorIPdfConverterIHtml2PdfServiceHtml2PdfDiagnostics
For advanced registration control, AddHtml2PdfWithLifetime(...) lets you override service lifetimes.
Browser Provisioning
By default, PuppeteerSharp can download Chromium on first use. For locked-down or pre-provisioned environments, configure the browser explicitly.
services.AddHtml2Pdf(options =>
{
options.MinInstances = 1;
options.MaxInstances = 5;
options.MaxLifetimeMinutes = 60;
options.AcquireTimeoutSeconds = 30;
options.Headless = true;
options.SkipBrowserDownload = true;
options.ExecutablePath = @"/usr/bin/chromium";
options.BrowserCacheDirectory = "/var/cache/nast-html2pdf";
});
Provisioning guidance:
- Keep
SkipBrowserDownload = falseif you want automatic Chromium provisioning. - Set
SkipBrowserDownload = trueonly when you manage Chromium yourself. - Use
ExecutablePathwhen your runtime image already contains Chrome/Chromium. - Use
BrowserCacheDirectorywhen you want a predictable PuppeteerSharp cache location. AdditionalArgsis available for environment-specific Chromium flags.
Explicit Opt-Ins
These settings are intentionally not enabled unless you request them.
services.AddHtml2Pdf(options =>
{
options.AllowInsecureCertificates = true;
options.DisableSandbox = true;
options.AdditionalArgs = ["--window-size=1280,720"];
});
Tradeoffs:
AllowInsecureCertificates = trueweakens TLS validation.DisableSandbox = truereduces browser isolation and should be reserved for environments that cannot run Chromium with the sandbox enabled.
Realistic Examples
Generate from Raw HTML
using Nast.Html2Pdf.Models;
var html = """
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<style>
body { font-family: Arial, sans-serif; margin: 32px; }
h1 { margin-bottom: 8px; }
.muted { color: #666; }
</style>
</head>
<body>
<h1>Quarterly Summary</h1>
<p class="muted">Generated from raw HTML</p>
</body>
</html>
""";
var pdfOptions = new PdfOptions
{
Format = "A4",
PrintBackground = true
};
var result = await html2Pdf.GeneratePdfFromHtmlAsync(html, pdfOptions);
if (result.Success && result.Data is not null)
{
await File.WriteAllBytesAsync("quarterly-summary.pdf", result.Data);
}
Generate from a Razor Template and Model
using Nast.Html2Pdf.Helpers;
var template = """
<!doctype html>
<html>
<body>
<h1>Invoice @Model.Number</h1>
<p>Customer: @Model.CustomerName</p>
<table>
<thead>
<tr><th>Description</th><th>Total</th></tr>
</thead>
<tbody>
@foreach (var item in Model.Items)
{
<tr><td>@item.Description</td><td>@item.Total.ToString("C")</td></tr>
}
</tbody>
</table>
<p><strong>Grand total: @Model.GrandTotal.ToString("C")</strong></p>
</body>
</html>
""";
var model = new
{
Number = "INV-2026-001",
CustomerName = "Ada Lovelace",
Items = new[]
{
new { Description = "Implementation work", Total = 250.00m },
new { Description = "Support", Total = 40.00m }
},
GrandTotal = 290.00m
};
var result = await html2Pdf.GeneratePdfAsync(
template,
model,
PdfOptionsHelper.Invoice,
HtmlOptionsHelper.Invoice);
Generate from a URL
using Nast.Html2Pdf.Models;
var result = await html2Pdf.GeneratePdfFromUrlAsync(
"https://example.com/report",
new PdfOptions
{
Format = "Letter",
PrintBackground = true,
WaitForImages = true,
TimeoutMs = 30000
});
Non-DI Usage
using Nast.Html2Pdf;
using Nast.Html2Pdf.Services;
var html2Pdf = Html2PdfFactory.Create(new BrowserPoolOptions
{
MaxInstances = 2,
AcquireTimeoutSeconds = 15
});
var result = await html2Pdf.GeneratePdfFromHtmlAsync("<html><body><h1>Hello</h1></body></html>");
PDF and HTML Configuration
Relevant models and helpers:
PdfOptions,PdfMargins,PdfHeaderFooterHtmlGenerationOptionsPdfOptionsHelperpresets likeA4Standard,Invoice,ReportHtmlOptionsHelperpresets likeStandard,Invoice,Report
Example:
using Nast.Html2Pdf.Models;
var pdfOptions = new PdfOptions
{
Format = "A4",
Landscape = false,
PrintBackground = true,
Margins = new PdfMargins
{
Top = "2cm",
Bottom = "2cm",
Left = "1.5cm",
Right = "1.5cm"
},
Header = new PdfHeaderFooter
{
Template = "<div style='font-size:10px;width:100%;text-align:center;'>Quarterly Summary</div>",
Height = "1cm"
}
};
var htmlOptions = new HtmlGenerationOptions
{
BasePath = AppContext.BaseDirectory,
InlineStyles = true,
IncludeViewport = true,
AdditionalCss = "body { color: #222; }"
};
Testing and CI
- Keep tests tightly scoped when touching pool behavior or documentation assertions.
- Hosted Linux CI may require disabling the Chromium sandbox for the test process only.
- This repository uses
HTML2PDF_TEST_DISABLE_SANDBOX=truein GitHub Actions test runs so browser-based tests can execute on hosted runners. - That CI opt-in does not change the library default: production code still keeps the sandbox enabled unless
DisableSandboxis explicitly set.
Examples:
dotnet test Tests/Nast.Html2Pdf.Tests.csproj --filter "FullyQualifiedName~BrowserPoolTests"
dotnet test Tests/Nast.Html2Pdf.Tests.csproj --filter "FullyQualifiedName~ProductionHardeningReleaseTests"
Documentation Links
License
| 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 was computed. 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 was computed. 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.7)
- Microsoft.Extensions.Logging (>= 9.0.7)
- Microsoft.Extensions.Options (>= 9.0.7)
- PuppeteerSharp (>= 20.2.0)
- RazorLight (>= 2.3.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.