QuestPDF.Markdown allows rendering markdown into a QuestPDF document using the markdig parser.
Important
QuestPDF.Markdown is not a HTML-to-PDF conversion library and does intend to become one. It aims to use markdown to add basic user-provided content into PDFs without the pitfalls of HTML-to-PDF conversion.
var text =
@"# Hello, world!
*Greetings* from **markdown**!
> Hello, back!";
var document = Document.Create(container =>
{
container.Page(page =>
{
page.Margin(20);
page.Content().Markdown(text);
});
});
The styling used by QuestPDF.Markdown can be configured using MarkdownRendererOptions
.
var text = @"> Hello, world!";
var options = new MarkdownRendererOptions
{
BlockQuoteBorderColor = Colors.Red.Medium,
BlockQuoteBorderThickness = 5
};
var document = Document.Create(container =>
{
container.Page(page =>
{
page.Margin(20);
page.Content().Markdown(text, options);
});
});
By default, downloading and rendering external images is disabled.
Images can be downloaded using the ParsedMarkdownDocument.DownloadImages()
method.
The parsed markdown document can then be passed to the Markdown()
extension method.
var text = @"![title](https://placehold.co/100x50.jpg)";
var markdown = ParsedMarkdownDocument.FromText(text);
await markdown.DownloadImages(httpClient: myHttpClient /* Optionally provide your own HttpClient */);
var document = Document.Create(container =>
{
container.Page(page =>
{
page.Margin(20);
page.Content().Markdown(markdown, options);
});
});
The aim of this library is to support all basic markdown functionality and some of the extensions supported by markdig.
Currently the following features are supported:
- Headings
- Block quotes
- Code blocks
- Lists (ordered, unordered)
- Emphasis (bold, italic)
- Task lists
- Extra emphasis (subscript, superscript, strikethrough, marked, inserted)
- Tables
- Images
- HTML encoded entities (e.g.
&
,<
,>
)
Support for the following extensions is currently not planned:
- Raw HTML
- Math blocks
- Diagrams
A full sample can be found in test.md and the resulting test.pdf.
To quickly test changes made in the library, you can make use of the excellent QuestPDF previewer in combination with the QuestPDF.Markdown.Tests project and dotnet watch
To render the test markdown file in the previewer, run the following command in the root directory of the repository:
dotnet watch test --project ./tests/QuestPDF.Markdown.Tests -- --filter Name=Render
To render the test markdown file in the previewer with additional background colors and margins, run the following command in the root directory of the repository:
dotnet watch test --project ./tests/QuestPDF.Markdown.Tests -- --filter Name=RenderDebug
Any changes made to the MarkdownRenderer class will be automatically reflected in the previewer.