A portable .NET library for converting Markdown, HTML, and plain text to PDF using embedded wkhtmltopdf. Windows-only, no external dependencies required.
$ dotnet add package MarkdownToPdf.NETA comprehensive .NET library for generating PDF files from Markdown and plain text with customizable styling and metadata support.
Inline code and code blocksBlockquotes with styling
Install the package via NuGet Package Manager:
dotnet add package PdfTools
Or via Package Manager Console:
Install-Package PdfTools
✅ No additional setup required! The package includes all necessary native dependencies for Windows, Linux, and macOS.
The library automatically handles the following dependencies:
<PackageReference Include="Markdig" Version="0.33.0" />
<PackageReference Include="DinkToPdf" Version="1.0.8" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
This package includes embedded native libraries for cross-platform support:
libwkhtmltox.dlllibwkhtmltox.so (when available)libwkhtmltox.dylib (when available)No separate installation of wkhtmltopdf is required!
using PdfTools.Services;
using PdfTools.Models;
// Create generator instance
var generator = new PdfGenerator();
// Generate PDF from markdown
string markdown = @"
# My Document
This is a **sample** document with *formatting*.
## Features
- Beautiful styling
- Easy to use
- Professional output
";
generator.GenerateFromMarkdown(markdown, "output.pdf");
// Generate PDF from plain text
string text = "This is plain text that will be converted to a nicely formatted PDF.";
generator.GenerateFromText(text, "text-output.pdf");var options = new PdfOptions
{
Title = "My Custom Document",
Author = "John Doe",
Subject = "Sample PDF Generation",
PageSize = "A4",
Orientation = "Portrait",
Margins = new PdfMargins
{
Top = 25,
Bottom = 25,
Left = 20,
Right = 20
},
CustomCss = @"
body { font-family: 'Times New Roman'; }
h1 { color: #ff6b6b; }
"
};
generator.GenerateFromMarkdown(markdown, "custom-output.pdf", options);// Get PDF as byte array (useful for web applications)
byte[] pdfBytes = generator.GenerateFromMarkdownToBytes(markdown, options);
// In ASP.NET Controller
return File(pdfBytes, "application/pdf", "document.pdf");using PdfTools.Extensions;
// In Program.cs or Startup.cs
builder.Services.AddPdfTools();
// Or with custom converter
builder.Services.AddPdfTools(provider =>
{
// Custom converter configuration
return new SynchronizedConverter(new PdfTools());
});[ApiController]
[Route("api/[controller]")]
public class PdfController : ControllerBase
{
private readonly IPdfGenerator _pdfGenerator;
public PdfController(IPdfGenerator pdfGenerator)
{
_pdfGenerator = pdfGenerator;
}
[HttpPost("from-markdown")]
public IActionResult GenerateFromMarkdown([FromBody] MarkdownRequest request)
{
try
{
var options = new PdfOptions
{
Title = request.Title,
Author = request.Author
};
var pdfBytes = _pdfGenerator.GenerateFromMarkdownToBytes(
request.Markdown, options);
return File(pdfBytes, "application/pdf", "document.pdf");
}
catch (Exception ex)
{
return BadRequest($"PDF generation failed: {ex.Message}");
}
}
}// Generate PDF from markdown to file
void GenerateFromMarkdown(string markdown, string outputPath, PdfOptions? options = null);
// Generate PDF from text to file
void GenerateFromText(string text, string outputPath, PdfOptions? options = null);
// Generate PDF from HTML to file
void GenerateFromHtml(string html, string outputPath, PdfOptions? options = null);
// Generate PDF from markdown to byte array
byte[] GenerateFromMarkdownToBytes(string markdown, PdfOptions? options = null);
// Generate PDF from text to byte array
byte[] GenerateFromTextToBytes(string text, PdfOptions? options = null);public class PdfOptions
{
public string? Title { get; set; } // PDF metadata title
public string? Author { get; set; } // PDF metadata author
public string? Subject { get; set; } // PDF metadata subject
public string Orientation { get; set; } // "Portrait" or "Landscape"
public string PageSize { get; set; } // "A4", "Letter", "Legal", etc.
public string? CustomCss { get; set; } // Custom CSS overrides
public PdfMargins Margins { get; set; } // Page margins
}
public class PdfMargins
{
public int Top { get; set; } // Top margin in mm
public int Bottom { get; set; } // Bottom margin in mm
public int Left { get; set; } // Left margin in mm
public int Right { get; set; } // Right margin in mm
}The library provides specific exception types for different error scenarios:
try
{
generator.GenerateFromMarkdown(markdown, outputPath);
}
catch (InvalidInputException ex)
{
// Handle invalid input (null/empty content, invalid paths)
Console.WriteLine($"Invalid input: {ex.Message}");
}
catch (PdfGenerationException ex)
{
// Handle PDF generation errors
Console.WriteLine($"PDF generation failed: {ex.Message}");
}
catch (FileOperationException ex)
{
// Handle file I/O errors
Console.WriteLine($"File operation failed: {ex.Message}");
}The library includes beautiful default CSS styling that provides:
You can override any styles using the CustomCss property in PdfOptions.
This library uses the following open-source packages:
Make sure to comply with their respective licenses in your applications.