Hi,
I would like to use Typst in my rust project.
I discover this crate : GitHub - Relacibo/typst-as-lib: Easily use typst from rust.
It lets the possibilities to use typst directly in rust code.
- I just take the sample.
- Add more data in content.
- And I try to generate a table with the input data.
I read documentation and try a lot to use table...
Is my typst template so bad or maybe this feature is not availble with the crate ?
Have you ever parameter a table in Typst ?
// main.rs
use derive_typst_intoval::{IntoDict, IntoValue};
use std::fs;
use typst::foundations::{Bytes, Dict, IntoValue};
use typst_as_lib::TypstEngine;
static TEMPLATE_FILE: &str = include_str!("./templates/template.typ");
//static FONT: &[u8] = include_bytes!("./fonts/texgyrecursor-regular.ttf");
static OUTPUT: &str = "./examples/output.pdf";
//static IMAGE: &[u8] = include_bytes!("./templates/images/typst.png");
fn main() {
// Read in fonts and the main source file.
// We can use this template more than once, if needed (Possibly
// with different input each time).
let template = TypstEngine::builder()
.main_file(TEMPLATE_FILE)
// .fonts([FONT])
.build();
// Run it
let doc = template
.compile_with_input(dummy_data())
.output
.expect("typst::compile() returned an error!");
// Create pdf
let options = Default::default();
let pdf = typst_pdf::pdf(&doc, &options).expect("Could not generate pdf.");
fs::write(OUTPUT, pdf).expect("Could not write pdf.");
}
// Some dummy content. We use `derive_typst_intoval` to easily
// create `Dict`s from structs by deriving `IntoDict`;
fn dummy_data() -> Content {
Content {
v: vec![
ContentElement {
heading: "Foo".to_owned(),
text: Some("Hello World!".to_owned()),
num1: 1,
num2: Some(42),
// image: Some(Bytes::new(IMAGE.to_vec())),
},
ContentElement {
heading: "Bar".to_owned(),
num1: 2,
..Default::default()
},
ContentElement {
heading: "Bar".to_owned(),
num1: 2,
..Default::default()
},
ContentElement {
heading: "Bar".to_owned(),
num1: 2,
..Default::default()
},
ContentElement {
heading: "Bar".to_owned(),
num1: 2,
..Default::default()
},
ContentElement {
heading: "Bar".to_owned(),
num1: 2,
..Default::default()
},
ContentElement {
heading: "Bar".to_owned(),
num1: 2,
..Default::default()
},
ContentElement {
heading: "Bar".to_owned(),
num1: 2,
..Default::default()
},
ContentElement {
heading: "Bar".to_owned(),
num1: 2,
..Default::default()
},
],
}
}
#[derive(Debug, Clone, IntoValue, IntoDict)]
struct Content {
v: Vec<ContentElement>,
}
// Implement Into<Dict> manually, so we can just pass the struct
// to the compile function.
impl From<Content> for Dict {
fn from(value: Content) -> Self {
value.into_dict()
}
}
#[derive(Debug, Clone, Default, IntoValue, IntoDict)]
struct ContentElement {
heading: String,
text: Option<String>,
num1: i32,
num2: Option<i32>,
// image: Option<Bytes>,
}
// template.typ
#import sys: inputs
#set page(paper: "a4", flipped: true)
#let content = inputs.v
#table (
columns : 5,
#for (i, elem) in content.enumerate() {
[#elem.heading],
[Text: #elem.text],
[Num1: #elem.num1],
[Num2: #elem.num2],
#if elem.image != none [#image.decode(elem.image, height: 40pt)],
}
)