|
| 1 | +# PeachPDF |
| 2 | +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. |
| 3 | + |
| 4 | +## PeachPDF Requirements |
| 5 | + |
| 6 | +- .NET 8 |
| 7 | + |
| 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_ |
| 9 | + |
| 10 | +## Installing PeachPDF |
| 11 | + |
| 12 | +Install the PeachPDF package from nuget.org |
| 13 | + |
| 14 | +``` |
| 15 | +dotnet add package PeachPDF |
| 16 | +``` |
| 17 | + |
| 18 | +## Using PeachPDF |
| 19 | + |
| 20 | +### Simple example |
| 21 | +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 |
| 22 | + |
| 23 | +```csharp |
| 24 | +PdfGenerateConfig pdfConfig = new(){ |
| 25 | + PageSize = PageSize.Letter, |
| 26 | + PageOrientation = PageOrientation.Portrait |
| 27 | +}; |
| 28 | + |
| 29 | +PdfGenerator generator = new(); |
| 30 | + |
| 31 | +var stream = new MemoryStream(); |
| 32 | + |
| 33 | +var document = await generator.GeneratePdf(html, pdfConfig); |
| 34 | +document.Save(stream); |
| 35 | +``` |
| 36 | + |
| 37 | +### Rendering an MHTML file |
| 38 | + |
| 39 | +You can generate PDF documents using self contained MHTML files (what Chrome calls "single page documents") by using the included MimeKitNetworkAdapter |
| 40 | + |
| 41 | +```csharp |
| 42 | +PdfGenerateConfig pdfConfig = new(){ |
| 43 | + PageSize = PageSize.Letter, |
| 44 | + PageOrientation = PageOrientation.Portrait |
| 45 | + NetworkAdapter = new MimeKitNetworkAdapter(File.OpenRead("example.mhtml")) |
| 46 | +}; |
| 47 | + |
| 48 | +PdfGenerator generator = new(); |
| 49 | + |
| 50 | +var stream = new MemoryStream(); |
| 51 | + |
| 52 | +// Passing null to GeneratePdf will load the HTML from the provided network adapter instance instead |
| 53 | +var document = await generator.GeneratePdf(null, pdfConfig); |
| 54 | +document.Save(stream); |
| 55 | +``` |
| 56 | + |
| 57 | +### Rending HTML from a URI |
| 58 | + |
| 59 | +You can also render HTML from the Internet to a PDF |
| 60 | + |
| 61 | +```csharp |
| 62 | +HttpClient httpClient = new(); |
| 63 | + |
| 64 | +PdfGenerateConfig pdfConfig = new(){ |
| 65 | + PageSize = PageSize.Letter, |
| 66 | + PageOrientation = PageOrientation.Portrait |
| 67 | + NetworkAdapter = new HttpClientNetworkADapter(httpClient, new Uri("https://www.example.com")) |
| 68 | +}; |
| 69 | + |
| 70 | +PdfGenerator generator = new(); |
| 71 | + |
| 72 | +var stream = new MemoryStream(); |
| 73 | + |
| 74 | +// Passing null to GeneratePdf will load the HTML from the provided network adapter instance instead |
| 75 | +var document = await generator.GeneratePdf(null, pdfConfig); |
| 76 | +document.Save(stream); |
| 77 | +``` |
| 78 | + |
| 79 | +## Fonts |
| 80 | + |
| 81 | +### Default Font |
| 82 | + |
| 83 | +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 |
| 84 | + |
| 85 | +```csharp |
| 86 | +PdfGenerator generator = new(); |
| 87 | +generator.AddFontFamilyMapping("Segoe UI","sans-serif"); // or any other system installed font |
| 88 | +``` |
| 89 | + |
| 90 | +### Adding custom fonts |
| 91 | + |
| 92 | +The recommended way to install custom fonts is to install them into your operating system. |
| 93 | +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) |
| 94 | + |
| 95 | +You can also add a font at runtime by loading the ttf font into a Stream, and then using the AddFontFromStream API: |
| 96 | + |
| 97 | +```csharp |
| 98 | +PdfGenerator generator = new(); |
| 99 | +await generator.AddFontFromStream(fontStream); // where fontStream is a System.IO.Stream of the loaded TTF file |
| 100 | +``` |
| 101 | + |
| 102 | +Web fonts loaded via @font-face are also supported. |
| 103 | + |
| 104 | +### Supported font formats |
| 105 | + |
| 106 | +We support any font supported by SixLabors.Fonts, currently TrueType, CFF, WOFF, and WOFF2 as of the time of this writing. |
0 commit comments