tfachmann / typst-as-library

Simple demo that demonstrates how to use typst as a library in Rust
Apache License 2.0
19 stars 1 forks source link

Typst as Library

As of March 15th, 2024 (typst version 0.11.0) it has finally been published to crates.io, so there is no more need for git dependencies.

This repository shows how to use typst as a library in Rust.

fn main() {
    let content = "= Hello, World!";

    // All the abstraction needed is here (!)
    let world = TypstWrapperWorld::new("./".to_owned(), content);

    // Render document
    let mut tracer = Tracer::default();
    let document = typst::compile(&world, &mut tracer).expect("Error compiling typst");

    // Output to pdf
    let pdf = typst_pdf::pdf(&document, None, None);
    fs::write("./output.pdf", pdf).expect("Error writing PDF.");
}

Acknowledgment

Code has been inspired by