Skip to content
PapyrinePublic

About

A .NET wrapper over krilla, the Rust PDF-writing library that backs typst. Creates PDF documents: pages, vector paths, gradients, text, and images, and converts HTML to PDF through the companion Krilla.Html package.

Resources

Stars

7 stars

Watchers

0 watching

Forks

Latest commit

 

History

241 Commits

Folders and files

Repository files navigation

Krilla

Build status NuGet Status

A .NET wrapper over krilla, the Rust PDF-writing library that backs typst. Creates PDF documents: pages, vector paths, gradients, text, raster images and SVG, and converts HTML to PDF through the companion Krilla.Html package.

Krilla writes PDFs. To read, render or edit an existing one, use Morph.PDFium.

Try it in the browser — a Blazor WebAssembly app that converts HTML to PDF entirely client side, with the Rust native linked into the app's own .wasm.

See Milestones for release notes.

NuGet packages

HTML to PDF

Krilla.Html converts HTML to PDF on top of this library. AngleSharp parses the markup and runs the CSS cascade, Krilla.Html lays the result out, and Krilla writes the PDF.

using var fonts = new FontSet()
    .AddDirectory(fontDirectory);

var pdf = await HtmlConverter.ConvertAsync(
    "<h1>Hello</h1><p>World</p>",
    new()
    {
        Fonts = fonts
    });

snippet source | anchor

Krilla has no font database, so the fonts a document may use are supplied by the caller rather than discovered from the host. That is what makes output reproducible across machines.

Implemented: block and inline layout, the box model including box-sizing, collapsing margins, line breaking, text alignment, pagination, tables, floats, relative and absolute positioning, overflow clipping, visibility, text-transform, letter and word spacing, the font-size keywords, text decorations with their colour and style, vertical-align including lengths and percentages, dashed, dotted and double borders, border-radius, opacity, transform, z-index and the stacking contexts that come with it, linear and radial gradients, outline, object-fit and object-position, both border models, empty-cells, images — raster and SVG alike — and links — <a href> becomes a real PDF link annotation, and a #fragment becomes an internal jump to wherever that element paginated to.

Flexbox too, in full: all four directions, wrapping, flex-grow, flex-shrink and flex-basis with the whole of the flexible-length resolution behind them, justify-content, align-items, align-content and align-self, order, gap, auto margins on both axes, baseline alignment, and the automatic minimum size that stops a shrinking item collapsing below its own longest word. Grid still lays out as a plain block.

Values resolve as CSS asks: calc(), the viewport units, and the whole length unit table. An inline element gets its own box model — background, padding, border and horizontal margins, one fragment per line — and a background may be a raster image, with background-repeat, -position, -size, -clip and -origin. Lines break at soft hyphens, at dashes, and inside a word where overflow-wrap or word-break permits it; tabs under pre advance to tab-size stops. A list-style-image marker is drawn from an image and falls back to the counter style when the source does not resolve.

An <svg> written into the document is drawn rather than laid out: its markup goes to the same renderer a referenced SVG does, so an icon beside a word or a chart in a report comes out as vector art. An <img src> or a CSS url() naming an SVG is rendered the same way, and sizes the way a browser sizes one: a document declaring width and height has an intrinsic size, while one carrying only a viewBox has an aspect ratio and no size — SVG defaults those attributes to 100% — so it fills its containing block and takes its height from the ratio. Text inside an SVG is shaped against the same fonts the document uses. An <image> inside an SVG is honoured only when its href is a data: URI: one naming a file resolves to nothing, on the same reasoning that keeps images from being fetched over the network.

Pagination honours every value the break properties take. page-break-before and page-break-after with a forced value start a new page, page-break-inside: avoid keeps a box whole by moving it to the next page rather than splitting it, a left or right value inserts the blank page it asks for, and avoid at a box edge moves the break to the declaring box's own edge — so a heading stays with the section under it rather than being stranded at the foot of a page. The modern break-* spellings are read as well.

A table's <thead> is re-drawn at the top of every page its table continues onto and its <tfoot> at the foot, and the rest of the table moves to make room — so page two of a long report is a labelled grid with its carried-forward line rather than an unlabelled one.

A position: fixed box is drawn at the same place on every page, which is how a running header or footer is written today — CSS 2.1 asks for exactly that in paged media, and it is what a browser's printer does. A box with neither top nor bottom is the one exception: its position comes from where flow put it, which is a position in the document rather than on a page, so it is drawn once and reported.

A document's @page rules decide the paper — size, orientation and margins — unless HtmlOptions.HonourPageRules says otherwise, and media queries resolve against print, so a @media print block is the one that applies.

@page's sixteen margin boxes carry running headers, footers and page numbers:

@page {
  margin: 25mm;
  @top-center { content: "Quarterly report" }
  @bottom-right { content: counter(page) " of " counter(pages) }
}
@page :first { @top-center { content: none } }

counter(page) and counter(pages) are resolved per sheet, the page selectors :first, :left, :right and :blank all select, and a margin box takes its own declarations rather than inheriting from the document. No browser implements any of this, so it is one of the few places this converter does more than the reference it is measured against.

string-set and string() work too, which is CSS's own way of putting a section's heading into a running header — h2 { string-set: title content() } beside @top-center { content: string(title) } heads every page with the section it starts in. So does page: cover and the @page cover rules it selects, for a document whose front matter wants a different header from its body.

orphans and widows constrain where a page breaks, on by default because a browser honours them too: a paragraph whose natural break would strand a single line moves whole to the next sheet, and one long enough to give a line up moves its break instead. A run too short to satisfy both counts keeps its break and splits, which is also what the browser does. HtmlOptions.HonourOrphansAndWidows turns them off, for a document whose lines are not prose.

Generated content works: ::before and ::after with strings, attr(), counter(), counters(), url() and the quote keywords, along with counter-reset, counter-increment and quotes.

::before and ::after take a display of their own: a block pseudo-element gets a box rather than joining its host's line, so content: ""; display: block; clear: both makes a container enclose its floats the way it always has.

The logical box properties are read — margin-inline, padding-block, inline-size and the rest — as is word-wrap, the spelling overflow-wrap had for a decade before it was renamed and the one most documents still carry.

Also aspect-ratio, rgba() on every colour property — text, backgrounds, borders, outlines, decorations and a collapsed table's rules — text and box shadows as offsets including inset, text-decoration-thickness and text-underline-offset, a percentage height, min-height or max-height against a containing block that has one, vertical-align: baseline on a table cell, <col> and <colgroup> widths, and border-style: hidden in a collapsed table.

Every value border-style takes is drawn the way a browser draws it, groove, ridge, inset and outset included — each in the two derived shades of the declared colour that CSS asks for and specifies nothing about. The ex and ch units resolve against the face rather than at an approximate half an em, and an absolutely positioned box with an offset at each end and auto margins is centred between them.

HTML's presentational attributes are mapped onto CSS, which AngleSharp does not do: <table width>, cellpadding, cellspacing, border, bgcolor, align, valign and nowrap, <hr>'s width, size, color, noshade and align, <font>'s color, size and face, <body bgcolor> and text, an image's align, border, hspace and vspace, and type on both kinds of list. They are hints rather than inline styles, so any author rule beats them. Documents converted to PDF come disproportionately from reporting tools and mail merges, which lay themselves out with exactly this markup.

A <wbr> offers a line break without forcing one, which is how a document says a long URL or a generated identifier may be split.

The PDF gets structure the page cannot show. Headings become a bookmark tree, nested by level and bounded by HtmlOptions.OutlineDepth; every id becomes a named destination, so report.pdf#introduction opens at that heading; and the document's <title> and lang fill the PDF's title and language when the caller has not set them.

HtmlOptions.Tagged adds a logical structure tree, which is what a screen reader navigates and what PDF/UA requires. HTML carries the semantics it wants, so headings, paragraphs, lists, tables, figures and their alternative text arrive with no extra markup — and everything that is not content is marked as an artifact, so a background, a border, a list marker or a repeated table header is never read out as if it were. It is off by default because it changes the bytes of every document.

Images resolve from data: URIs and from files relative to BaseUrl. Nothing is fetched over the network by default — converting an untrusted document would otherwise issue requests to whatever hosts it names. Set HtmlOptions.ImageResolver to take that decision explicitly, and the two policies to bound what any resolver may load:

options.LocalImages = ImagePolicy.SafeDirectories(assetDirectory);
options.WebImages = ImagePolicy.SafeDomains("cdn.example.com");

snippet source | anchor

Both are checked before the resolver runs, so a refused source is never requested. A data: URI is never gated, since its bytes are already in the document.

Because the engine implements a subset of CSS and lays the rest out as a plain block, a document using an unimplemented construct comes out wrong with nothing to say so. OnDiagnostic turns that into a report:

options.OnDiagnostic = diagnostic => Console.WriteLine(diagnostic);

// <@font-face> src: Corporate — not read, so the document's own font is not loaded
// <div> display: grid — laid out as a block
// <div> column-count: 2 — laid out in one column
// <table> rules: all — not applied, because presentational attributes are not mapped onto CSS
// <img> src: logo.png — did not resolve to an image, so no box was generated
// <p> bidirectional text: ש (U+05E9) — laid out left to right, so it comes out in the wrong order

snippet source | anchor

Unrecognised CSS is deliberately not reported. Listing every cursor and content an ordinary stylesheet carries would bury the signal, and would cost the invariant that makes the sink worth subscribing to: a conversion that reports nothing laid out every construct the way a browser would.

Limits worth knowing before reaching for it:

  • Text is shaped through krilla's own shaper, so kerning and ligatures are applied, and a character the resolved face lacks is drawn in a registered face that covers it. Bidirectional resolution and complex-script shaping are still missing, so a run is shaped in one direction and one script. A document holding right-to-left text says so through the sink.
  • Lines break at spaces, at hyphens and dashes, at a soft hyphen or a <wbr>, and either side of an image or inline-block. There is no hyphenation dictionary and no Unicode line breaking algorithm beyond that — so scripts that wrap without spaces overflow rather than wrapping, and say so.
  • A document's own @font-face rules are reported and not loaded. Register the faces through HtmlOptions.Fonts instead: a font is a document resource with the same exfiltration concern an image has, and loading one on the document's say-so would need a policy of its own.
  • AngleSharp compares CSS specificity across cascade origins, where the specification resolves origin first. A reset relying on * { margin: 0 } will not clear the default margins on body and p; name the elements explicitly instead.
  • A gradient's corner keyword is rewritten as 45deg by the cascade before it reaches the engine, so linear-gradient(to top right, …) runs at 45° rather than at the angle the box's proportions call for. Give an explicit angle where it matters.

The default stylesheet AngleSharp ships is the HTML 4.01 one, which disagrees with browsers on most block elements — headings, paragraph spacing, and list indentation in particular. Krilla.Html appends corrections from the HTML Standard's rendering section, so an unstyled document matches a browser.

Layout fidelity is measured against Chrome scenario by scenario — see the corpus.

A specimen page

showcase.html is one hand-written document that combines most of the above at once: a positioned stamp on the masthead, a floated side panel that shortens the lines beside it, justified and shaped text, a bar chart drawn entirely out of boxes and percentage widths, a table, and a real link annotation in the footer. The picture is page one of the PDF, rendered through PDFium at 96 dpi. Click it for the PDF itself.

A page of the specimen document converted to PDF

Both artefacts are produced by ShowcaseTests, which also asserts the conversion reports no diagnostics — so nothing on that page was laid out as something other than what it asks for.

Usage

Every snippet below is a test, and the image under it is that test's snapshot: the first page of the PDF the code actually produces, rendered through PDFium. Click one for the full PDF. Both are regenerated whenever the sample changes, so the code and the picture cannot drift apart.

Hello world

using var document = new KrillaDocument();

using (var page = document.StartPage(PageSettings.A4))
{
    page.Surface.FillRectangle(
        Rectangle.FromSize(50, 50, 200, 100),
        Color.Rgb(220, 40, 40));
}

var pdf = document.Finish();

snippet source | anchor

Result

Rendered output of the HelloWorld sample

Draw a path

using var document = new KrillaDocument();
using var paint = Paint.Solid(Color.Rgb(30, 90, 200));

using (var page = document.StartPage(300, 200))
{
    using var path = new PathBuilder()
        .MoveTo(20, 20)
        .LineTo(280, 20)
        .LineTo(150, 180)
        .Close()
        .Build();

    page.Surface
        .SetFill(paint)
        .DrawPath(path);
}

var pdf = document.Finish();

snippet source | anchor

Result

Rendered output of the DrawAPath sample

Fill and stroke

Fill and stroke are independent pieces of surface state. Setting both draws both; setting neither makes krilla fall back to filling black rather than drawing nothing.

using var document = new KrillaDocument();
using var fill = Paint.Solid(Color.Rgb(250, 220, 120));
using var outline = Paint.Solid(Color.Rgb(60, 60, 60));

using (var page = document.StartPage(200, 200))
{
    using var path = PdfPath.Rectangle(Rectangle.FromSize(40, 40, 120, 120));

    // Fill and stroke are independent state; setting both draws both.
    page.Surface
        .SetFill(fill)
        .SetStroke(new Stroke(outline, Width: 4, DashArray: [8, 4]))
        .DrawPath(path);
}

var pdf = document.Finish();

snippet source | anchor

Result

Rendered output of the FillAndStroke sample

Gradients

using var document = new KrillaDocument();
using var gradient = Paint.LinearGradient(
    0, 0, 300, 0,
    [
        new(0f, Color.Rgb(255, 90, 0)),
        new(1f, Color.Rgb(0, 90, 255))
    ]);

using (var page = document.StartPage(300, 150))
{
    using var path = PdfPath.Rectangle(new(0, 0, 300, 150));
    page.Surface.SetFill(gradient).DrawPath(path);
}

var pdf = document.Finish();

snippet source | anchor

All stops in a gradient must share one colour space. A mismatch is reported when the document is finished, not when the gradient is created.

Result

Rendered output of the GradientFill sample

Transforms, clips and opacity

These use a push/pop stack, which krilla requires to be balanced. Each push returns a Layer whose disposal pops it, so a using statement makes the pairing structural.

using var document = new KrillaDocument();

using (var page = document.StartPage(200, 200))
{
    var surface = page.Surface;

    // Each push is reverted when its layer is disposed, so the pairing krilla
    // requires is structural rather than something to remember.
    using (surface.PushTransform(Matrix3x2.CreateTranslation(100, 100)))
    using (surface.PushOpacity(0.5f))
    {
        surface.FillRectangle(new(-50, -50, 50, 50), Color.Rgb(0, 160, 90));
    }
}

var pdf = document.Finish();

snippet source | anchor

Result

Rendered output of the TransformsAndOpacity sample

Multiple pages

using var document = new KrillaDocument();

foreach (var index in Enumerable.Range(0, 3))
{
    using var page = document.StartPage(PageSettings.Letter);
    page.Surface.FillRectangle(
        Rectangle.FromSize(72, 72 * (index + 1), 200, 40),
        Color.Gray(80));
}

document.Save("report.pdf");

snippet source | anchor

Result

Rendered output of the MultiplePages sample

Fonts and text

krilla has no font database, so Krilla carries one. Font.Find("Arial", bold: true) scans the installed fonts once, lazily, and returns the closest face, or null when the family is not installed. A FontRepository over a fixed set of directories replaces the installed fonts, and setting FontRepository.Default makes Font.Find use it everywhere:

  • The system and per-user font folders on Windows, /System/Library/Fonts, /Library/Fonts and ~/Library/Fonts on macOS, and the fontconfig directories plus the XDG defaults on Linux. FontRepository.SystemDirectories lists them.
  • A family matches a face's typographic or legacy family name, in any language the font carries, ignoring case. A full or PostScript name (Arial Bold, Arial-BoldMT) finds that one face.
  • The PDF Standard-14 names map to the faces readers substitute: Helvetica to Arial, Times-Roman to Times New Roman, Courier to Courier New. Suffixes carry the style, so Helvetica-BoldOblique is Arial Bold Italic. Those three families then fall back to their metric-compatible stand-ins, Liberation and Arimo, Tinos and Cousine, which is what a Linux machine usually has.
  • The closest style wins, so a family with no bold face returns its regular one, and FontFaceInfo.Bold and .Italic say what was found. FallbackFamily names a family to use when nothing matches.
  • .ttc and .otc collections are read face by face. FontRepository.FindFace returns where a face is without loading it.

Font.MeasureText gives the width of a shaped line, kerning included. Font.WrapText breaks text into lines that fit a width, ready to draw one by one with DrawText. It breaks at spaces, and inside a word only when the word alone is too wide.

// Font.Find("Arial", bold: true) searches the installed fonts. A repository over a
// fixed directory pins them, so the output is the same on every machine.
var fonts = new FontRepository(fontDirectory);
using var title = fonts.Find("Helvetica", bold: true)!;
using var body = fonts.Find("Helvetica", italic: true)!;

const string text =
    "Helvetica is a Standard-14 name, so it finds Arial or a face metric-compatible " +
    "with it. WrapText breaks at spaces, and inside a word only when the word alone " +
    "is wider than the line.";

using var document = new KrillaDocument();

using (var page = document.StartPage(300, 200))
{
    page.Surface
        .SetFill(Color.Rgb(20, 20, 20))
        .DrawText(new(20, 40), title, 20, "List of Documents");

    float baseline = 70;
    foreach (var line in body.WrapText(text, 12, 260))
    {
        page.Surface.DrawText(new(20, baseline), body, 12, line);
        baseline += 16;
    }
}

var pdf = document.Finish();

snippet source | anchor

Result

Rendered output of the Text sample

Images

using var document = new KrillaDocument();

// Raw RGBA, four bytes per pixel, row-major.
var pixels = new byte[4 * 4 * 4];

for (var y = 0; y < 4; y++)
{
    for (var x = 0; x < 4; x++)
    {
        var offset = (y * 4 + x) * 4;
        var dark = (x + y) % 2 == 0;

        pixels[offset] = dark ? (byte) 40 : (byte) 230;
        pixels[offset + 1] = dark ? (byte) 110 : (byte) 230;
        pixels[offset + 2] = dark ? (byte) 190 : (byte) 230;
        pixels[offset + 3] = 255;
    }
}

using var image = PdfImage.FromRgba(pixels, 4, 4);

using (var page = document.StartPage(120, 120))
{
    page.Surface.DrawImage(image, Rectangle.FromSize(10, 10, 100, 100));
}

var pdf = document.Finish();

snippet source | anchor

Result

Rendered output of the DrawAnImage sample

Metadata

using var document = new KrillaDocument();

document.SetMetadata(
    new()
    {
        Title = "Quarterly Report",
        Language = "en-GB",
        Authors = ["A. Writer"],
        Keywords = ["quarterly", "report"],
        CreationDate = created
    });

using (var page = document.StartPage(PageSettings.A4))
{
    page.Surface.FillRectangle(new(72, 72, 523, 130), Color.Gray(40));
}

var pdf = document.Finish();

snippet source | anchor

Result

Rendered output of the Metadata sample

Bookmarks

using var document = new KrillaDocument();

foreach (var index in Enumerable.Range(0, 3))
{
    using var page = document.StartPage(PageSettings.A4);
    page.Surface.FillRectangle(
        Rectangle.FromSize(72, 72, 200 + index * 60, 32),
        Color.Gray((byte) (40 + index * 60)));
}

var chapter = new OutlineItem("Chapter One", pageIndex: 0)
{
    IsOpen = true
};
chapter.Add("Section 1.1", pageIndex: 1);

document.SetOutline(chapter, new OutlineItem("Chapter Two", pageIndex: 2));

var pdf = document.Finish();

snippet source | anchor

Result

Rendered output of the Bookmarks sample

Links

using var document = new KrillaDocument();

using (var page = document.StartPage(PageSettings.A4))
{
    var surface = page.Surface;

    // Nothing about a link is visible on its own, so draw the text it sits over.
    surface.FillRectangle(new(72, 72, 300, 100), Color.Rgb(20, 80, 200));
    surface.AddLink(new(72, 72, 300, 100), "https://example.com/");

    surface.FillRectangle(new(72, 120, 300, 148), Color.Rgb(20, 80, 200));
    // Internal — the target page need not exist yet.
    surface.AddLink(new(72, 120, 300, 148), pageIndex: 1);
}

using (document.StartPage(PageSettings.A4))
{
}

var pdf = document.Finish();

snippet source | anchor

Result

Rendered output of the Links sample

Attachments

using var document = new KrillaDocument();

using (var page = document.StartPage(PageSettings.A4))
{
    page.Surface.FillRectangle(new(72, 72, 523, 160), Color.Gray(200));
}

document.EmbedFile(
    "source-data.csv",
    "name,value\nalpha,1\n"u8,
    mimeType: "text/csv",
    description: "The data behind the chart",
    association: FileAssociation.Data);

var pdf = document.Finish();

snippet source | anchor

Result

Rendered output of the Attachments sample

Reusable graphics

Content captured once and drawn repeatedly costs almost nothing in file size: krilla emits the stream once and references it. The same mechanism backs soft masks (PushMask) and tiling patterns (CapturePattern).

using var document = new KrillaDocument();

using (var page = document.StartPage(PageSettings.A4))
{
    // Captured once, emitted once, referenced many times.
    using var stamp = page.Surface.CaptureGraphic(
        surface => surface.FillRectangle(new(0, 0, 40, 40), Color.Rgb(200, 30, 30)));

    foreach (var index in Enumerable.Range(0, 20))
    {
        using (page.Surface.PushTransform(Matrix3x2.CreateTranslation(72 + index * 20, 72)))
        {
            page.Surface.DrawGraphic(stamp);
        }
    }
}

var pdf = document.Finish();

snippet source | anchor

Result

Rendered output of the ReusableGraphic sample

PDF/A

using var document = new KrillaDocument(
    new()
    {
        Archival = PdfArchival.A2B,
        XmpMetadata = true
    });

document.SetMetadata(
    new()
    {
        Title = "Archived Invoice",
        Language = "en-GB",
        CreationDate = created
    });

using (var page = document.StartPage(PageSettings.A4))
{
    page.Surface.FillRectangle(new(72, 72, 523, 200), Color.Gray(220));
}

// Conformance violations are reported here, as a KrillaException, rather than when
// the offending content was added.
var pdf = document.Finish();

snippet source | anchor

Result

Rendered output of the ArchivalPdf sample

Tagged PDF and PDF/UA

An accessible PDF needs a logical structure tree: a hierarchy of headings, paragraphs, lists and tables that a screen reader navigates, separate from the drawing order. Mark spans of content as they are drawn, then assemble the identifiers into a tree.

using var document = new KrillaDocument(
    new()
    {
        EnableTagging = true,
        Accessibility = PdfAccessibility.Ua1
    });

// PDF/UA requires a title, a language and an outline. Omitting any of them fails at
// Finish, with the specific rule named in the exception.
document.SetMetadata(
    new()
    {
        Title = "An Accessible Document",
        Language = "en-GB"
    });

TagIdentifier headingContent;
TagIdentifier bodyContent;

using (var page = document.StartPage(PageSettings.A4))
{
    var surface = page.Surface;

    // Each tagged span yields an identifier that goes into the structure tree.
    headingContent = surface.BeginText();
    surface.FillRectangle(new(72, 72, 523, 100), Color.Gray(30));
    surface.EndTagged();

    bodyContent = surface.BeginText();
    surface.FillRectangle(new(72, 120, 523, 400), Color.Gray(160));
    surface.EndTagged();
}

using var tree = new TagTree();
tree.WithLanguage("en-GB");

var section = tree.Add(TagKind.Section);
section.Add(Tag.Heading(1, "Introduction")).Add(headingContent);
section.Add(TagKind.Paragraph).Add(bodyContent);

document.SetTagTree(tree);
document.SetOutline(new OutlineItem("Introduction", pageIndex: 0));

var pdf = document.Finish();

snippet source | anchor

Content that is decoration rather than meaning — running heads, page numbers, rules — should be marked with BeginArtifact instead, which keeps it out of the tree entirely.

Result

Rendered output of the AccessibleDocument sample

Behavior notes

  • The surface origin is the top-left corner, with Y increasing downward. PDF's own coordinate system is Y-up; krilla applies the flip.
  • Sizes are in points — 72 to the inch. PageSettings.A4 is 595 x 842 and PageSettings.Letter is 612 x 792.
  • One page can be open at a time. krilla keeps global serialization state while a page is open, so starting a second page before closing the first throws. Page is IDisposable, so a using statement handles it.
  • Finish() consumes the document. Nothing can be added afterwards.
  • A document with no pages still produces a valid PDF: krilla writes a single empty page, because a page-less PDF is invalid.
  • krilla has no font database, and does not match on family or style. Font.Load takes bytes; Font.Find and FontRepository find them by family and style, from a scan written in managed code. See Fonts and text.
  • Documents are not thread safe and carry no internal locking — unlike Morph.PDFium, which serializes on a process-wide lock because PDFium has global state. Krilla has none, so separate documents are genuinely independent and can be built in parallel; a single document must be used from one thread at a time.
  • Output is deterministic for a given Krilla version, which makes it suitable for snapshot testing.

Native binaries

The package carries runtimes/<rid>/native/ entries for nine RIDs: win-x64, win-arm64, linux-x64, linux-arm64, linux-musl-x64, linux-musl-arm64, osx-x64, osx-arm64 and browser-wasm. NuGet copies only the one matching the target runtime, so dotnet publish -r linux-x64 emits a single libkrilla_capi.so.

A runtime-agnostic dotnet build copies the whole tree into bin. To avoid that, set a runtime identifier:

<PropertyGroup>
  <RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>

The Linux builds are linked against glibc 2.17, so they load on every currently supported distribution. Separate musl builds are shipped because .NET's RID graph does not fall back from linux-musl-x64 to linux-x64; without them an Alpine container would fail with a bare DllNotFoundException.

Building from source needs Rust — dotnet build shells out to cargo when the native is missing or stale, and skips silently when cargo is absent. Consumers of the package never need Rust.

WebAssembly

browser-wasm is the odd one out, and how it differs is worth knowing before reaching for it. There is no dynamic loader in a browser for a P/Invoke to search, so the shim ships as a static archive — krilla_capi.a — which is linked into the application's own .wasm module while it builds. The package hands it to that link step, so a package reference and a runtime identifier are the whole of the setup:

<PropertyGroup>
  <RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
</PropertyGroup>

Two things follow from being linked rather than loaded.

The wasm-tools workload is required, because the application is relinked from source rather than assembled out of prebuilt parts:

dotnet workload install wasm-tools

Expect the build to take appreciably longer than a managed-only one. That is the relink, and it is charged whether or not Krilla is the reason for it.

The application's .wasm grows by roughly the size of a PDF engine, because that is what is being linked in. A stock Blazor WebAssembly app publishes a 2.9 MB dotnet.native.wasm; the same app with Krilla publishes 7.4 MB, compressed to well under half that in transit by the .br files the SDK emits alongside. Worth knowing before putting a PDF writer in a page that has to load quickly.

WebAssembly exception handling has to stay switched on, which it is by default. Every export in the shim contains its panics inside catch_unwind, so a failure inside krilla comes back as a status code rather than taking the runtime down with it, and on this target the compiler implements that with native WebAssembly exception handling. <WasmEnableExceptionHandling>false</WasmEnableExceptionHandling> therefore fails the link on an undefined __cpp_exception symbol — the package raises a message naming the property before the linker gets that far.

Fonts have to arrive as bytes. krilla has no font database, and a browser has no directory for FontSet.AddDirectory to read, so fetch each face over HTTP and load it with FontFace.Load. Build the set once and keep it: it owns native handles, and the faces are megabytes, not a download to repeat per conversion.

var set = new FontSet();
foreach (var face in faces)
{
    set.Add(FontFace.Load(await http.GetByteArrayAsync($"fonts/{face}")));
}

src/Krilla.Web is a complete Blazor WebAssembly app built this way, and is what the live demo runs.

browser-wasm is also the only 32-bit target here. Nothing in the public API changes: the interop layer is written in pointer-sized types throughout, so the struct layouts follow the target on their own, and the shipped archive is checked against the managed side's expectations on every release build.

Images are worth a thought on this platform in particular. Krilla never fetches over the network on any target — that is a security default rather than a gap — so pictures and fonts arrive as bytes the caller supplies, which in a browser means fetching them in application code before handing them over.

Third-party licences

Krilla itself is MIT. The native library statically links krilla and its dependency tree, so their licences are reproduced in THIRD-PARTY-NOTICES.md, which ships inside the package.

Icon

Icon attribution pending — the placeholder is currently shared with Morph.PDFium and must be replaced before the first public release.

About

A .NET wrapper over krilla, the Rust PDF-writing library that backs typst. Creates PDF documents: pages, vector paths, gradients, text, and images, and converts HTML to PDF through the companion Krilla.Html package.

Resources

Stars

7 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages