MarkdownToPdf.NET
1.0.1
See the version list below for details.
dotnet add package MarkdownToPdf.NET --version 1.0.1
NuGet\Install-Package MarkdownToPdf.NET -Version 1.0.1
<PackageReference Include="MarkdownToPdf.NET" Version="1.0.1" />
<PackageVersion Include="MarkdownToPdf.NET" Version="1.0.1" />
<PackageReference Include="MarkdownToPdf.NET" />
paket add MarkdownToPdf.NET --version 1.0.1
#r "nuget: MarkdownToPdf.NET, 1.0.1"
#:package MarkdownToPdf.NET@1.0.1
#addin nuget:?package=MarkdownToPdf.NET&version=1.0.1
#tool nuget:?package=MarkdownToPdf.NET&version=1.0.1
PdfTools Library
A comprehensive .NET library for generating PDF files from Markdown and plain text with customizable styling and metadata support.
Features
- ✅ Convert Markdown to PDF with full formatting support
- ✅ Convert plain text to PDF with proper paragraph formatting
- ✅ Direct HTML to PDF conversion
- ✅ Portable and self-contained - no external dependencies required
- ✅ Cross-platform support - Windows, Linux, macOS
- ✅ Customizable PDF metadata (title, author, subject)
- ✅ Configurable page settings (size, orientation, margins)
- ✅ Modern, readable default styling
- ✅ Custom CSS override support
- ✅ Dependency injection support for ASP.NET applications
- ✅ Comprehensive error handling
- ✅ Logging integration
- ✅ Both file output and byte array output options
Supported Markdown Features
- Headers (H1-H6) with styled borders
- Bold and italic text formatting
- Unordered and ordered lists
- Links
Inline code
and code blocks-
Blockquotes with styling
- Tables with borders
- Horizontal rules
- Emoji support 😊
- Task lists
- And more via Markdig extensions
Installation
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.
Dependencies
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" />
Portable Native Libraries
This package includes embedded native libraries for cross-platform support:
- Windows:
libwkhtmltox.dll
- Linux:
libwkhtmltox.so
(when available) - macOS:
libwkhtmltox.dylib
(when available)
No separate installation of wkhtmltopdf is required!
Quick Start
Basic Usage
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");
With Custom Options
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);
Generate to Byte Array
// 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");
ASP.NET Integration
Startup Configuration
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());
});
Controller Usage
[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}");
}
}
}
API Reference
IPdfGenerator Interface
// 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);
PdfOptions Configuration
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
}
Error Handling
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}");
}
Styling
The library includes beautiful default CSS styling that provides:
- Modern, readable fonts (Segoe UI family)
- Proper heading hierarchy with colored borders
- Styled lists and blockquotes
- Syntax-highlighted code blocks
- Professional table formatting
- Print-optimized layout
You can override any styles using the CustomCss
property in PdfOptions
.
License
This library uses the following open-source packages:
Make sure to comply with their respective licenses in your applications.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net6.0
- DinkToPdf (>= 1.0.8)
- Markdig (>= 0.33.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 7.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.