typst-bake
Bake Typst templates, fonts, and packages into your Rust binary — use Typst as a self-contained, embedded library.
Features
- Multi-Format Output - Generate PDF, SVG, or PNG from the same template
- Simple API - Generate documents with just
document!("main.typ").to_pdf() - Minimal Setup - Just specify
template-dirandfonts-dirinCargo.tomlmetadata - File Embedding - All files in
template-dirare embedded and accessible from templates - Font Embedding - Fonts (TTF, OTF, TTC) in
fonts-dirare automatically bundled into the binary - Automatic Package Bundling - Scans for package imports, downloads them at compile time, and recursively resolves all dependencies
- Package Caching - Downloaded packages are cached in system cache directory for faster compilation. Use
TYPST_BAKE_REFRESH=1to force re-download - Runtime Inputs - Pass dynamic data from Rust structs to Typst via
IntoValue/IntoDictderive macros - Optimized Binary Size - Resources are compressed with zstd and decompressed lazily at runtime
- Smart Recompilation - File changes trigger recompilation automatically, with optional build script for complete coverage
Installation
Add to your Cargo.toml:
[]
= "0.1"
[]
= "./templates" # Path to your .typ files and assets
= "./fonts" # Path to your font files
Cargo Features
| Feature | Description |
|---|---|
pdf (default) |
Enable to_pdf() |
svg |
Enable to_svg() |
png |
Enable to_png() |
full |
Enable all output formats |
PDF works out of the box. To disable PDF and use only SVG: default-features = false, features = ["svg"].
Optional: Complete File Change Detection
By default, typst-bake detects template or font file modifications and triggers recompilation when you run cargo build. File additions and deletions are not detected directly, but this is rarely an issue—adding a new file usually requires modifying an existing file (like main.typ) to use it, which triggers recompilation anyway.
If you want to fully detect even the rare case where you only add or remove files without modifying existing ones, add a build script:
# Cargo.toml
[]
= "build.rs"
[]
= "0.1"
// build.rs
Quick Start
For a complete walkthrough, see the Quick Start Guide (PDF). Also check out the PDF outputs from other examples below—each document explains its usage in detail.
Examples
| Example | Description | Command | Output |
|---|---|---|---|
| quick-start | Generates the Quick Start PDF using all features | cargo run -p example-quick-start |
|
| font-guide | Guide to font setup and usage | cargo run -p example-font-guide |
|
| with-inputs | Pass dynamic data from Rust to Typst | cargo run -p example-with-inputs |
|
| with-files | Embed images and various data files | cargo run -p example-with-files |
|
| with-package | Automatic package bundling | cargo run -p example-with-package |
|
| output-formats | Multi-format output with rendering test patterns | cargo run -p example-output-formats |
See below |
output-formats Example Outputs
| Format | Files |
|---|---|
| output.pdf (42KB) | |
| SVG | output_1.svg (306KB), output_2.svg (700KB) |
| PNG | output_1.png (388KB), output_2.png (693KB) |
Comparison with typst-as-lib
typst-as-lib is a lightweight and flexible wrapper that makes it easy to use the Typst compiler as a Rust library. It supports various combinations of runtime filesystem access, package downloads from the internet, caching, and more.
typst-bake uses typst-as-lib internally, adding a layer focused on creating self-contained binaries. This focused scope enables a simple, easy-to-use API. It embeds all resources (templates, fonts, packages) into the binary at compile time, so the resulting executable works anywhere without external files or network access. For packages, the entire process—scanning, downloading, compressing, and embedding—is fully automatic.
Key Differences
| Aspect | typst-as-lib | typst-bake |
|---|---|---|
| Resources | Runtime filesystem access or compile-time individual file embedding | Embeds entire folders at compile time |
| Packages | Runtime download (with caching) or local filesystem | Automatic scan, download, compress, and embed at compile time |
| Fonts | Typst default fonts, embedded fonts, system fonts, etc. | Embedded fonts only |
| Configuration | Flexible setup via builder pattern in code | Cargo.toml metadata only |
| API | Flexible with fine-grained control | Simple (document!("main.typ").to_pdf()) |
Which should you use?
- If you want all resources embedded in your binary for a fully self-contained executable → use typst-bake
- If you prefer runtime flexibility (e.g., downloading packages on demand to reduce binary size) → use typst-as-lib directly
License
MIT OR Apache-2.0