MdToPdf.Renderer 6.2.0-preview-2

This is a prerelease version of MdToPdf.Renderer.
dotnet add package MdToPdf.Renderer --version 6.2.0-preview-2
                    
NuGet\Install-Package MdToPdf.Renderer -Version 6.2.0-preview-2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MdToPdf.Renderer" Version="6.2.0-preview-2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MdToPdf.Renderer" Version="6.2.0-preview-2" />
                    
Directory.Packages.props
<PackageReference Include="MdToPdf.Renderer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MdToPdf.Renderer --version 6.2.0-preview-2
                    
#r "nuget: MdToPdf.Renderer, 6.2.0-preview-2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MdToPdf.Renderer@6.2.0-preview-2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MdToPdf.Renderer&version=6.2.0-preview-2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=MdToPdf.Renderer&version=6.2.0-preview-2&prerelease
                    
Install as a Cake Tool

MdToPdf.Renderer

NuGet License: MIT

A .NET 8 library that converts Markdown to PDF using PdfSharp 6.x. No headless browser, no external process - a pure managed pipeline from Markdown string to PdfDocument.

Features

  • Headings - ATX-style H1-H6 with proportional sizing
  • Inline formatting - bold, italic, inline code, and combinations
  • Fenced code blocks - monospace font with a shaded background
  • Blockquotes - with a left accent bar
  • Lists - unordered and ordered, including nested levels
  • Hyperlinks - external URLs and #anchor internal links with PDF annotations
  • Images - local file paths and data: URIs
  • Thematic breaks - rendered as a thin horizontal rule
  • Auto-bookmarks - PDF outline built automatically from heading hierarchy
  • Password protection - user/owner passwords with granular permissions
  • Compression - DEFLATE content-stream compression
  • Progress reporting - callback with 0-100 percent as pages are rendered
  • Per-page error handling - continue or abort on render failures
  • Custom fonts - register TTF bytes or load a folder at startup

Installation

dotnet add package MdToPdf.Renderer

Usage

Minimal

using MdToPdf.Renderer;

using var doc = PdfGenerator.GeneratePdf(markdown, new PdfOptions
{
    PageSize = PageSize.A4,
    MarginTop = 40, MarginBottom = 40,
    MarginLeft = 50, MarginRight = 50
});
doc.Save("output.pdf");

Fluent builder

var bytes = await PdfGenerator.Create()
    .WithPageSize(PageSize.A4)
    .WithMargin(40)
    .WithBookmarks()
    .WithCompression()
    .GeneratePdfAsync(markdown);

await File.WriteAllBytesAsync("output.pdf", bytes);

Async (returns byte[])

byte[] bytes = await PdfGenerator.GeneratePdfAsync(markdown, options);

PdfOptions reference

  • PageSize (XSize, default A4) - use PageSize.A4, Letter, Legal, A3
  • PageOrientation (default Portrait) - Portrait or Landscape
  • MarginTop / MarginBottom / MarginLeft / MarginRight (double, default 0) - page margins in points
  • BaseFontSize (double, default 11) - body text size in points
  • BodyFontFamily (string, default "Arial") - font family for body text
  • MonospaceFontFamily (string, default "Courier New") - font family for code blocks
  • LineHeight (double, default 1.4) - line height multiplier
  • AutoBookmarks (bool, default false) - build PDF outline from headings
  • CompressOutput (bool, default false) - DEFLATE compress content streams
  • Security (PdfSecurityOptions?, default null) - password and permissions
  • OnProgress (Action<int>?) - callback with percent 0–100
  • OnRenderError (Action<Exception>?) - per-page error callback
  • ContinueOnError (bool, default false) - skip failed pages instead of throwing
  • ImageLoadFailureMode (default RenderAltText) - RenderAltText or Throw

Auto-bookmarks

var doc = PdfGenerator.Create()
    .WithBookmarks()
    .GeneratePdf(markdown);

Headings become a nested PDF outline. H1 at the root, H2 nested under the preceding H1, and so on. Internal #anchor links in the Markdown resolve to heading positions via slug matching.

Password protection

var options = new PdfOptions
{
    Security = new PdfSecurityOptions
    {
        UserPassword  = "open",
        OwnerPassword = "admin",
        Permissions   = PdfPermissions.Print | PdfPermissions.CopyContent
    }
};

PdfPermissions flags: Print, HighQualityPrint, ModifyContent, CopyContent, Annotate, FillForms, AssembleDocument, All, ReadOnly, None.

Compression

var doc = PdfGenerator.Create()
    .WithCompression()
    .GeneratePdf(markdown);

Enables DEFLATE compression on all content streams. Produces smaller files at the cost of slightly longer generation time.

Progress and error handling

var options = new PdfOptions
{
    OnProgress     = pct => Console.WriteLine($"Rendering: {pct}%"),
    OnRenderError  = ex  => logger.LogError(ex, "Page render failed"),
    ContinueOnError = true
};

OnProgress fires at 0 % before the first page and at (page / total) × 100 after each page. When ContinueOnError is true, a failed page is left blank and rendering continues; otherwise the exception propagates after calling OnRenderError.

Custom fonts

// Register a single font
byte[] fontBytes = File.ReadAllBytes("MyFont-Regular.ttf");
PdfGenerator.Create()
    .WithFont("MyFont", fontBytes)
    .GeneratePdf(markdown);

// Or load a whole folder at startup
PdfGenerator.LoadFontsFromFolder("/app/fonts");

On Windows, common system fonts (Arial, Calibri, Segoe UI, Verdana, etc.) are resolved automatically. Custom fonts registered via WithFont take precedence over system mapping.

License

MIT. PDF rendering by PdfSharp (MIT, see NOTICE.txt).

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
6.2.0-preview-2 69 4/26/2026
6.2.0-preview-1 58 4/24/2026