PeachPDF
Peach PDF is a pure .NET HTML -> PDF rendering library
Install / Use
/learn @jhaygood86/PeachPDFREADME
PeachPDF
Peach PDF is a pure .NET HTML -> PDF rendering library. This library does not depend on Puppeter, wkhtmltopdf, or any other process to render the HTML to PDF. As a result, this should work in virtually any environment where .NET 8+ works. As a side benefit of being pure .NET, performance improvements in future .NET versions immediately benefit this library.
PeachPDF Requirements
- .NET 8
Note: This package depends on PeachPDF.PdfSharpCore and various SixLabors libraries. Both have their own licenses, but the end result is still open source
Installing PeachPDF
Install the PeachPDF package from nuget.org
dotnet add package PeachPDF
Using PeachPDF
Simple example
Simple example to render PDF to a Stream. All images and assets must be local to the file on the file system or in data: URIs
PdfGenerateConfig pdfConfig = new(){
PageSize = PageSize.Letter,
PageOrientation = PageOrientation.Portrait
};
PdfGenerator generator = new();
var stream = new MemoryStream();
var document = await generator.GeneratePdf(html, pdfConfig);
document.Save(stream);
Rendering an MHTML file
You can generate PDF documents using self contained MHTML files (what Chrome calls "single page documents") by using the included MimeKitNetworkAdapter
PdfGenerateConfig pdfConfig = new(){
PageSize = PageSize.Letter,
PageOrientation = PageOrientation.Portrait
NetworkAdapter = new MimeKitNetworkAdapter(File.OpenRead("example.mhtml"))
};
PdfGenerator generator = new();
var stream = new MemoryStream();
// Passing null to GeneratePdf will load the HTML from the provided network adapter instance instead
var document = await generator.GeneratePdf(null, pdfConfig);
document.Save(stream);
Rending HTML from a URI
You can also render HTML from the Internet to a PDF
HttpClient httpClient = new();
PdfGenerateConfig pdfConfig = new(){
PageSize = PageSize.Letter,
PageOrientation = PageOrientation.Portrait
NetworkAdapter = new HttpClientNetworkADapter(httpClient, new Uri("https://www.example.com"))
};
PdfGenerator generator = new();
var stream = new MemoryStream();
// Passing null to GeneratePdf will load the HTML from the provided network adapter instance instead
var document = await generator.GeneratePdf(null, pdfConfig);
document.Save(stream);
Note that loading images using relative paths will default to local file system unless a RNetworkLoader instance is provided (such as HttpClientNetworkAdapter) that sets the BaseUri property or if the HTML has a <base> element with an href property set. Images will need to be in the current working directory.
Fonts
Default Font
By default, PeachPDF uses Segoe UI. Segoe UI is installed by default on Windows, but isn't necessarily available on other platforms. You can remap Segoe UI to another font using
PdfGenerator generator = new();
generator.AddFontFamilyMapping("Segoe UI","sans-serif"); // or any other system installed font
Adding custom fonts
The recommended way to install custom fonts is to install them into your operating system. PeachPDF by default picks up TrueType fonts from the operating system (%SystemRoot%\Fonts and %LOCALAPPDATA%\Microsoft\Windows\Fonts on Windows, /Library/Fonts on Mac, and /usr/share/fonts, /usr/local/share/fonts/, and $HOME/.fonts on Linux)
You can also add a font at runtime by loading the ttf font into a Stream, and then using the AddFontFromStream API:
PdfGenerator generator = new();
await generator.AddFontFromStream(fontStream); // where fontStream is a System.IO.Stream of the loaded TTF file
Web fonts loaded via @font-face are also supported.
Supported font formats
We support any font supported by SixLabors.Fonts, currently TrueType, CFF, WOFF, and WOFF2 as of the time of this writing.
Related Skills
node-connect
344.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
96.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
344.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
344.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
