Markdown2Pdf

VB.NET 2025-07-28

Markdown2Pdf


.NET library for converting Markdown to PDF. Uses Markdig for converting markdown to HTML and then Puppeteer Sharp to convert that output to PDF.

For a cross-platform cli-application using this package checkout Markdown2Pdf.Console.

A full demo PDF can be found here!

Usage

C#:

var converter = new Markdown2PdfConverter();
var resultPath = await converter.Convert("README.md");

VB.NET:

Dim converter As New Markdown2PdfConverter()
Dim resultPath As String = Await converter.Convert("README.md")

An enumeration of markdown files can also be passed to the converter, combining them into one PDF.

Wiki

Checkout the Wiki for more documentation.

Options

To further specify the conversion process, pass Markdown2PdfOptions to the converter:

var options = new Markdown2PdfOptions {
  HeaderHtml = "
"document-title" style="background-color: #5eafed; width: 100%; padding: 5px">
"
, FooterHtml = "
"background-color: #5eafed; width: 100%; padding: 5px">Page "pageNumber">/"totalPages">
"
, DocumentTitle = "Example PDF", }; var converter = new Markdown2PdfConverter(options);

Alternatively the Markdown2PdfOptions can be loaded from a YAML Front Matter block at the start of the markdown file:

var converter = Markdown2PdfConverter.CreateWithInlineOptionsFromFile("README.md");
var resultPath = await converter.Convert("README.md");

Usage examples for this can be found here.

Option Description
ChromePath Path to chrome or chromium executable or self-downloads it if null.
CodeHighlightTheme The theme to use for highlighting code blocks.
CustomHeadContent A string containing any content valid inside a HTML to apply extra scripting / styling to the document.
DocumentTitle The title of this document. Can be injected into the header / footer by adding the class document-title to the element.
EnableAutoLanguageDetection Auto detect the language for code blocks without specfied language.
FooterHtml HTML-string to use as the document footer.
Format The paper format for the PDF.
HeaderHtml HTML-string to use as the document header.
IsLandscape Paper orientation.
KeepHtml true if the created HTML should not be deleted.
MarginOptions Css-margins for the sides of the document.
ModuleOptions Options that decide from where to load additional modules. More Information.
Scale Scale of the content. Must be between 0.1 and 2.
TableOfContents Creates a TOC from the markdown headers. More Information.
Theme The styling to apply to the document.

Table of contents

To add a table of contents insert

  • [TOC] (Gitlab Syntax)
  • [[_TOC_]] (Gitlab Syntax)
  • or (Comment)

into the markdown document and use the Markdown2PdfOptions.TableOfContents option:

# My Document

[TOC]
...

Example creating a TOC:

options.TableOfContents = new TableOfContentsOptions {
  ListStyle = ListStyle.Decimal,

  // Include all heading levels from 2 to 4.
  MinDepthLevel = 2,
  MaxDepthLevel = 4
};

A header can be omitted from the toc by ending it with :

## This header won't be displayed in the TOC 

The TOC gets generated within a

下载源码

通过命令行克隆项目:

git clone https://github.com/Flayms/Markdown2Pdf.git