Skip to content

Rust

The lamina crate is the façade over the core crates. Add it as a path dependency, then build a document. This is the hello example, whose bytes are checked against fixtures/golden/examples/hello.pdf in CI:

crates/lamina/examples/hello.rs
//! The smallest document: one A4 page with a line of text in a standard 14 font.
//!
//! ```sh
//! cargo run -p lamina --example hello -- [out-dir] # writes <out-dir>/hello.pdf (default target/examples-out)
//! ```
use std::error::Error;
use std::path::PathBuf;
use lamina::{BuildOptions, Builder, CancelToken, PageSize, PointPt, Pt, SaveOptions, Standard14};
fn main() -> Result<(), Box<dyn Error>> {
let out = PathBuf::from(std::env::args().nth(1).unwrap_or_else(|| "target/examples-out".into()));
std::fs::create_dir_all(&out)?;
std::fs::write(out.join("hello.pdf"), build()?)?;
Ok(())
}
/// The document's bytes: the same on every run and every platform.
pub(crate) fn build() -> Result<Vec<u8>, Box<dyn Error>> {
// Everything that would come from the environment (file ID, dates) is given.
let options = BuildOptions::default()
.with_id(*b"lamina-ex-hello!")
.with_producer("lamina examples")
.with_creation_date("D:20261030120000Z".parse()?);
let mut b = Builder::new(options);
let helvetica = b.standard_font(Standard14::Helvetica)?;
let mut page = b.page(PageSize::A4);
// Page space: points from the top-left corner, y down; `y` is the text's baseline.
let _ = page.surface().text(
"Hello, world!",
&helvetica,
Pt::new(24.0),
PointPt::from_points(72.0, 96.0),
)?;
page.finish()?;
let mut doc = b.finish()?;
Ok(doc.save(&SaveOptions::new(), &CancelToken::new())?.bytes)
}

Run it with cargo run -p lamina --example hello, which writes target/examples-out/hello.pdf.

  • Everything from the environment is given: the file ID, the producer and the dates. The output is the same on every run and every platform.
  • CancelToken: long work (saving, jobs) takes a token, so another thread can stop it. save_job returns a job that you step through with a work budget instead.
  • The Rust API reference documents every public item.