typst-as-lib
Small wrapper for typst that makes it easier to use it as a templating engine. Maybe useful for someone...
Usage
rust code
use derive_typst_intoval::{IntoDict, IntoValue};
use std::fs;
use typst::foundations::{Bytes, Dict, IntoValue, Smart};
use typst::text::Font;
use typst_as_lib::TypstTemplate;
static TEMPLATE_FILE: &str = include_str!(
"./templates/template.typ"
);
static FONT: &[u8] = include_bytes!(
"./fonts/texgyrecursor-regular.otf"
);
fn main() {
let font = Font::new(Bytes::from(FONT), 0)
.expect("Could not parse font!");
let template = TypstTemplate::new(vec![font], TEMPLATE_FILE);
let content = Content {
v: vec![
ContentElement {
heading: "Heading".to_owned(),
text: Some("Text".to_owned()),
num1: 1,
num2: Some(2),
},
ContentElement {
heading: "Heading2".to_owned(),
num1: 2,
..Default::default()
},
],
};
let mut tracer = Default::default();
let doc = template
.compile_with_input(&mut tracer, content)
.expect("typst::compile() returned an error!");
let pdf = typst_pdf::pdf(&doc, Smart::Auto, None);
fs::write("./output.pdf", pdf).expect("Could not write pdf.");
}
impl From<Content> for Dict {
fn from(value: Content) -> Self {
value.into_dict()
}
}
#[derive(Debug, Clone, IntoValue, IntoDict)]
struct Content {
v: Vec<ContentElement>,
}
#[derive(Debug, Clone, Default, IntoValue, IntoDict)]
struct ContentElement {
heading: String,
text: Option<String>,
num1: i32,
num2: Option<i32>,
}
typst code
// template.typ
#import sys: inputs
#set page(paper: "a4")
#set text(font: "TeX Gyre Cursor", 11pt)
#let content = inputs.v
#let last_index = content.len() - 1
#for (i, elem) in content.enumerate() [
== #elem.heading
Text: #elem.text \
Num1: #elem.num1 \
Num2: #elem.num2 \
#if i < last_index [
#pagebreak()
]
]
Run example with cargo r --example small_example.
Some links, idk