Skip to main content

Crate typst_bake

Crate typst_bake 

Source
Expand description

§typst-bake

Bake Typst templates, fonts, and packages into your Rust binary — use Typst as a self-contained, embedded library.

§Cargo Features

PDF is enabled by default. To use only SVG: default-features = false, features = ["svg"].

§Features

  • Simple API - Set template-dir and fonts-dir in Cargo.toml, then generate documents with just document!("main.typ").to_pdf()
  • Multi-Format Output - Generate PDF, SVG, or PNG with optional page selection
  • Self-Contained Binary - Templates, fonts, and packages are all embedded into the binary at compile time. No external files or internet connection needed at runtime
  • Automatic Package Resolution - Just use #import "@preview/..." as in Typst. Packages are resolved automatically using Typst’s own cache and data directories
  • Runtime Inputs - Pass dynamic data from Rust structs to Typst via IntoValue / IntoDict derive macros
  • Runtime Files - Inject files at runtime with Document::add_file for dynamically generated content or downloaded resources
  • Optimized Binary Size - Embedded resources are deduplicated and compressed automatically

§Quick Start

Add to your Cargo.toml:

[dependencies]
typst-bake = "0.1"

[package.metadata.typst-bake]
template-dir = "./templates"
fonts-dir = "./fonts"

Then use the document! macro:

let doc = typst_bake::document!("main.typ");

let pdf = doc.to_pdf()?;
std::fs::write("output.pdf", &pdf)?;

let svgs = doc.to_svg()?;
std::fs::write("page1.svg", &svgs[0])?;

let pngs = doc.to_png(144.0)?; // 144 DPI
std::fs::write("page1.png", &pngs[0])?;

Macros§

document
Creates a Document with embedded templates, fonts, and packages.

Structs§

CategoryStats
Statistics for a category of files (templates, fonts).
DedupStats
Statistics for content deduplication across all categories.
Document
A fully self-contained document ready for rendering.
EmbedStats
Compression statistics for all embedded content.
PackageInfo
Statistics for a single package.
PackageStats
Statistics for all packages.
Pages
A lightweight view into a Document with a page selection filter.
PdfConfigpdf
PDF export configuration for Document::with_pdf_config.
PdfTimestamppdf
A PDF creation timestamp.

Enums§

Error
Errors that can occur during document compilation and rendering.
PdfStandardpdf
A PDF conformance standard to enforce on export.

Traits§

HasCompressionRatio
Trait for types that have original/compressed sizes and can compute a compression ratio.

Functions§

rebuild_if_changed
Emits cargo:rerun-if-changed directives for template and font directories.

Type Aliases§

Result
A specialized Result type for typst-bake operations.

Derive Macros§

IntoDict
Derive macro for converting a struct to a Typst dictionary.
IntoValue
Derive macro for converting a struct to a Typst value.