Skip to content

Markdown2Pdf.Markdown2PdfConverter

GitHub Action edited this page Mar 24, 2025 · 9 revisions

Class Markdown2PdfConverter

Namespace: Markdown2Pdf
Assembly: Markdown2Pdf.dll

The main class for converting markdown to PDF.

public class Markdown2PdfConverter : IConvertionEvents

Inheritance

objectMarkdown2PdfConverter

Implements

IConvertionEvents

Inherited Members

object.Equals(object), object.Equals(object, object), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object, object), object.ToString()

Examples

The following code example shows how to convert a file "README.md" in the current directory to PDF. The output will be saved as "README.pdf" in the same directory.

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

To further specify the conversion process, Markdown2PdfOptions can be passed to the converter.

var options = new Markdown2PdfOptions {
  HeaderHtml = File.ReadAllText("header.html"),
  FooterHtml = File.ReadAllText("footer.html"),
  DocumentTitle = "Example PDF",
};
var converter = new Markdown2PdfConverter(options);

Constructors

Markdown2PdfConverter(Markdown2PdfOptions?)

Instantiates a new Markdown2PdfConverter.

public Markdown2PdfConverter(Markdown2PdfOptions? options = null)

Parameters

options Markdown2PdfOptions?

Optional options to specify how to convert the markdown.

Properties

ContentTemplate

The template used for generating the HTML which then gets converted into PDF.

public string ContentTemplate { get; set; }

Property Value

string

Remarks

Modify this to get more control over the HTML generation (e.g. to add your own JS-Scripts).

Options

Contains all options this converter uses for generating the PDF.

public Markdown2PdfOptions Options { get; }

Property Value

Markdown2PdfOptions

Remarks

Can be set with the constructor Markdown2PdfOptions).

OutputFileName

The PDF file name without extension.

public string? OutputFileName { get; }

Property Value

string?

PipelineBuilder

The Markdig MarkdownPipelineBuilder used for the markdown to HTML conversion.

public MarkdownPipelineBuilder PipelineBuilder { get; }

Property Value

MarkdownPipelineBuilder

Remarks

This MarkdownPipelineBuilder has the following extensions enabled by default:
* MarkdownPipelineBuilder)
* MarkdownPipelineBuilder)
* Boolean)
* AutoLink

Methods

Convert(FileInfo)

Converts the given markdown-file to PDF.

public Task<FileInfo> Convert(FileInfo markdownFile)

Parameters

markdownFile FileInfo

FileInfo containing the markdown.

Returns

Task<FileInfo>

The newly created PDF file.

Remarks

The PDF will be saved in the same location as the markdown file with the naming convention "markdownFileName.pdf".

Convert(FileInfo, FileInfo)

Converts the given markdown-file to PDF.

public Task Convert(FileInfo markdownFile, FileInfo outputFile)

Parameters

markdownFile FileInfo

FileInfo containing the markdown.

outputFile FileInfo

FileInfo for saving the generated PDF.

Returns

Task

Convert(string)

Converts the given markdown file to PDF.

public Task<string> Convert(string markdownFilePath)

Parameters

markdownFilePath string

Path to the markdown file.

Returns

Task<string>

Filepath to the generated pdf.

Remarks

The PDF will be saved at the path specified in outputFilePath.

Convert(string, string)

Converts the given markdown file to PDF.

public Task<string> Convert(string markdownFilePath, string outputFilePath)

Parameters

markdownFilePath string

Path to the markdown file.

outputFilePath string

File path for saving the PDF to.

Returns

Task<string>

Filepath to the generated pdf.

Remarks

The PDF will be saved at the path specified in outputFilePath.

Convert(IEnumerable<string>)

Converts the given enumerable of markdown files to PDF.

public Task<string> Convert(IEnumerable<string> markdownFilePaths)

Parameters

markdownFilePaths IEnumerable<string>

Enumerable with paths of the markdown files.

Returns

Task<string>

Remarks

The PDF will be saved in the same location of the first markdown file with the naming convention "markdownFileName.pdf".

Convert(IEnumerable<string>, string)

Converts the given enumerable of markdown files to PDF.

public Task<string> Convert(IEnumerable<string> markdownFilePaths, string outputFilePath)

Parameters

markdownFilePaths IEnumerable<string>

Enumerable with paths of the markdown files.

outputFilePath string

File path for saving the PDF to.

Returns

Task<string>

CreateWithInlineOptionsFromFile(string)

Instantiates a new Markdown2PdfConverter. The Markdown2PdfOptions are loaded from a YAML front matter block at the start of the given markdown document.

public static Markdown2PdfConverter CreateWithInlineOptionsFromFile(string markdownFilePath)

Parameters

markdownFilePath string

Path to the markdown file containing the YAML front matter.

Returns

Markdown2PdfConverter

The new Markdown2PdfConverter.

Examples

Use this at the beginning of the markdown file:

---
document-title: myDocumentTitle
metadata-title: myMetadataTitle
module-options: Remote # or None or path to node_module directory
theme: Github # or Latex or None or path to css file
code-highlight-theme: Github
enable-auto-language-detection: true
header-html: "&lt;div class='document-title' style='background-color: #5eafed; width: 100%; padding: 5px'&gt;&lt;/div&gt;"
# footer-html: "&lt;div&gt;hello world&lt;/div&gt;"
# custom-head-content: "&lt;style&gt;h2 { page-break-before: always; }&lt;/style&gt;"
# chrome-path: "C:\Program Files\Google\Chrome\Application\chrome.exe"
keep-html: false
margin-options:
  top: 80px
  bottom: 50px
  left: 50px
  right: 50px
is-landscape: false
format: A4
scale: 1
table-of-contents:
  list-style: decimal
  min-depth-level: 2
  max-depth-level: 6
  page-number-options:
    tab-leader: dots
---

# Here the normal markdown content starts

Remarks

Instead of three dashes (---) an HTML comment (<!-- -->) can also be used to wrap the YAML.

CreateWithInlineOptionsFromFile(FileInfo)

Instantiates a new Markdown2PdfConverter. The Markdown2PdfOptions are loaded from a YAML front matter block at the start of the given markdown document.

public static Markdown2PdfConverter CreateWithInlineOptionsFromFile(FileInfo markdownFile)

Parameters

markdownFile FileInfo

Markdown file containing the YAML front matter.

Returns

Markdown2PdfConverter

The new Markdown2PdfConverter.

Examples

Use this at the beginning of the markdown file:

---
document-title: myDocumentTitle
metadata-title: myMetadataTitle
module-options: Remote # or None or path to node_module directory
theme: Github # or Latex or None or path to css file
code-highlight-theme: Github
enable-auto-language-detection: true
header-html: "&lt;div class='document-title' style='background-color: #5eafed; width: 100%; padding: 5px'&gt;&lt;/div&gt;"
# footer-html: "&lt;div&gt;hello world&lt;/div&gt;"
# custom-head-content: "&lt;style&gt;h2 { page-break-before: always; }&lt;/style&gt;"
# chrome-path: "C:\Program Files\Google\Chrome\Application\chrome.exe"
keep-html: false
margin-options:
  top: 80px
  bottom: 50px
  left: 50px
  right: 50px
is-landscape: false
format: A4
scale: 1
table-of-contents:
  list-style: decimal
  min-depth-level: 2
  max-depth-level: 6
  page-number-options:
    tab-leader: dots
---

# Here the normal markdown content starts

Remarks

Instead of three dashes (---) an HTML comment (<!-- -->) can also be used to wrap the YAML.

BeforeHtmlConversion

Gets invoked before the markdown to HTML conversion.

public event EventHandler<MarkdownArgs>? BeforeHtmlConversion

Event Type

EventHandler<MarkdownArgs>?

OnTempPdfCreatedEvent

Gets invoked after a temporary PDF file is created.

public event EventHandler<PdfArgs>? OnTempPdfCreatedEvent

Event Type

EventHandler<PdfArgs>?

Remarks

This only happens if a parsing of the generated PDF is needed, e.g. for generating page numbers.

OnTemplateModelCreating

Gets invoked when the template model is created.

public event EventHandler<TemplateModelArgs>? OnTemplateModelCreating

Event Type

EventHandler<TemplateModelArgs>?

Remarks

This can be used to add custom content to the html template.

Clone this wiki locally