PeachPDF

PeachPDF

PdfGenerator Class

Renders HTML (and optionally CSS) into a PDF document entirely in-process, with no external browser or process dependency. This is the main entry point for PeachPDF: use one of the GeneratePdf overloads to create a new PeachPdfDocument, or one of the AddPdfPages overloads to append rendered pages to an existing one.

public class PdfGenerator

Inheritance System.Object → PdfGenerator

Remarks

A PdfGenerator instance is not thread-safe. Its brush/pen caches, font resolver, and network loader are owned exclusively by that instance, so calling its methods concurrently from multiple threads on the same instance (or reusing one instance across overlapping renders) is not supported and can corrupt its internal state. This includes every custom font registered on it — via @font-face or AddFontFromStream(Stream) — whose resolved glyph/metrics data lives in caches private to that instance, so two instances that register different bytes under the same font family name never collide.

Using a separate instance per thread — e.g. one per incoming web request, or one per work item in a parallel batch — is safe and is the intended way to generate PDFs concurrently. Pure system-font data (fonts already installed on the machine, discovered once at process startup) is deliberately the one exception: it’s immutable and safely shared read-only across every instance, so resolving e.g. “Arial Bold” isn’t repeated per instance.

Methods  
AddFontFamilyMapping(string, string) Adds a font mapping from fromFamily to toFamily iff the fromFamily is not found.
When the fromFamily font is used in rendered html and is not found in existing fonts (installed or added) it will be replaced by toFamily.
AddFontFromStream(Stream) Add a font to be rendered
AddFontFromStream(Stream, IReadOnlyList<RuneRange>) Add a font to be rendered, restricting it to the given codepoint ranges - the programmatic equivalent of an @font-faceunicode-range descriptor. Characters outside these ranges resolve to another registered font (per-codepoint font matching). Pass an empty list to register a font that is never selected by codepoint coverage.
AddPdfPages(PeachPdfDocument, string, PageSize, int, PeachPdfCssContent) Create PDF pages from given HTML and appends them to the provided PDF document.
AddPdfPages(PeachPdfDocument, string, PdfGenerateConfig, PeachPdfCssContent) Create PDF pages from given HTML and appends them to the provided PDF document.
GeneratePdf(string, PageSize, int, PeachPdfCssContent) Create PDF document from given HTML.
GeneratePdf(string, PdfGenerateConfig, PeachPdfCssContent) Create PDF document from given HTML.
ParseStyleSheet(string, bool) Parses the given stylesheet into a reusable PeachPdfCssContent object.
If combineWithDefault is true the parsed css blocks are added to the default css data (as defined by the CSS 2.1 default stylesheet for HTML), merged if class name already exists. If false only the data in the given stylesheet is returned.