Part of DKNet.Svc.PdfGenerators. ConvertHtmlAsync renders a raw HTML string, and
ConvertHtmlFileAsync reads an HTML file first — both skip the Markdig step entirely and hand the content straight
to Chromium.
using DKNet.Svc.PdfGenerators;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddPdfGenerator();
await using var provider = services.BuildServiceProvider();
var generator = provider.GetRequiredService<IPdfGenerator>();
// raw HTML string -> {CurrentDirectory}/output_from_html.pdf
var fromString = await generator.ConvertHtmlAsync("<h1>Report</h1><p>Generated directly from HTML.</p>");
Console.WriteLine(fromString);
// an HTML file on disk; throws FileNotFoundException if it does not exist
var fromFile = await generator.ConvertHtmlFileAsync("page.html", "page.pdf");
Console.WriteLine(fromFile);
Run with dotnet run.
| Call | Output when the path argument is omitted |
|---|---|
ConvertHtmlAsync |
{CurrentDirectory}/output_from_html.pdf |
ConvertHtmlFileAsync |
same default; throws FileNotFoundException first if the input file is missing |
Convert Markdown to a PDF renders Markdig output through this same Chromium pipeline;
Page layout, header and footer covers the PdfGeneratorOptions fields both paths
share. Browser lifecycle and request safety covers what happens to
<img>, <script>, and other requests the rendered HTML makes.
SetContentAsync, not loaded from a base URL, so images and stylesheets need absolute URLs or inline data (data: URIs).