typstgenv0.6.2

Rust and Wasm

The public API of the crate and the Wasm export. Item-level documentation lives on docs.rs.

The full API documentation is on docs.rs/typstgen.

Rust

cargo add typstgen --no-default-features
use typstgen::{compile, Config};

let config = Config::load(None)?;
let pdf_bytes = compile("example.typ".as_ref(), &config)?;
std::fs::write("example.pdf", pdf_bytes)?;
Item Purpose
compile(input: &Path, config: &Config) -> Result<Vec<u8>> Compiles a .typ file and returns the PDF bytes. Warnings are discarded.
compile_with_warnings(input: &Path, config: &Config) -> Result<Output> Same, but returns an Output with pdf, the page count pages and the formatted warnings.
compile_with_options(input: &Path, config: &Config, options: &CompileOptions) -> Result<Output> Same, with sys.inputs and PDF settings.
render_png(input, config, options, page, pixels_per_point) -> Result<Vec<u8>> Renders one 1-based page as PNG (png feature). 1.0 pixel per point is 72 dpi.
CompileOptions inputs, pdf_standards, creation_timestamp, pages, pdf_tags; see Inputs and PDF options. Default exports a plain, tagged PDF.
PdfStandard PDF version, PDF/A or PDF/UA-1. Parses from and displays as 1.7, a-2b, ua-1 …; PdfStandard::ALL lists them.
PageRange Inclusive 1-based range with optional ends. Parses from 5, 1-3, -3, 8-.
Config template_paths, font_paths, use_system_fonts and the package settings; see typstgen.toml. Implements Default, Serialize and Deserialize.
Config::load(Option<&Path>) Reads the given file, else ./typstgen.toml, else the defaults.
Config::template_roots() The configured template directories that exist, canonicalized, in order.
Error, Result Error type and alias used by all of the above.
use typstgen::{compile_with_options, CompileOptions, PdfStandard};

let options = CompileOptions {
    inputs: [("customer".to_owned(), "ACME".to_owned())].into(),
    pdf_standards: vec![PdfStandard::A2b],
    creation_timestamp: Some(1_767_225_600), // 2026-01-01T00:00:00Z
    ..Default::default()
};
let output = compile_with_options("invoice.typ".as_ref(), &config, &options)?;

Config can also be built in code, which is what the test suite does:

let config = Config {
    template_paths: vec!["templates".into()],
    font_paths: vec![],
    use_system_fonts: false,
    ..Config::default()
};

Invoices, profiles and archives

These modules sit behind Cargo features that are on by default (zugferd, archive); with --no-default-features, enable the ones you need.

Item Purpose
zugferd::xml_from_docgen(invoice_json, company_json) -> Result<String> Checks a business-templates invoice and returns its CII XML (EN 16931). Pass the bytes as CompileOptions::zugferd_xml with PdfStandard::A3b to embed them as factur-x.xml. See E-invoices.
zugferd::ubl_xml_from_docgen(format, invoice_json, company_json) -> Result<String> The same invoice as UBL 2.1 XML; format is UblFormat::XRechnung30 or UblFormat::PeppolBis30.
invoice_config::prepare_invoice(config, request) -> Value Invoice amounts, tax groups and rendering data from a user-owned configuration.
profiles::list_document_profiles, resolve_document_profile, validate_document Profile listing, selection and invoice validation against a package catalog. See Document profiles.
archive::compile_and_record, archive::verify Record builds in a manifest and verify them later. See Archiving.

Errors

Variant When
InputNotFound The input file does not exist.
Config The config file cannot be read.
InvalidConfig The config file is not valid TOML for Config.
Compile Typst reported errors; the message lists them as path:line:column: error: … with their hints, followed by any warnings.
PageOutOfRange render_png was asked for a page the document does not have.
Zugferd The invoice data has problems; the message lists one per line, starting with the JSON field to fix.
Archive A manifest cannot be read, written or verified.
Io Any other I/O error, for example reading the input.

Wasm

cargo build --target wasm32-unknown-unknown --no-default-features --features wasm

The wasm feature exports one function through wasm-bindgen. Generate the JavaScript bindings with wasm-bindgen or wasm-pack as for any wasm-bindgen crate; typstgen does not publish an npm package.

// Bindings generated with: wasm-bindgen --target web --out-dir pkg \
//   target/wasm32-unknown-unknown/release/typstgen.wasm
import init, { compile } from './pkg/typstgen.js';

await init();
const pdf = compile(JSON.stringify({
  source: '#import "templates/shared.typ": title\n#title',
  files: { 'templates/shared.typ': '#let title = [Hello]' },
}));
  • source is the main document, files maps virtual paths to their contents.
  • Optional: inputs (object of strings for sys.inputs), pdf_standards (for example ["a-2b"]), creation_timestamp (Unix seconds), pages (for example "1-3,5") and pdf_tags (boolean, default true), as in Inputs and PDF options.
  • File contents are strings, so only text files (Typst sources, CSV, JSON …) can be passed.
  • The result is the PDF as a Uint8Array. Invalid JSON, an invalid path or a Typst error throws with the message.
  • Only the bundled fonts are available. datetime.today() only has a date when creation_timestamp is set.

Edit this page on GitHub · Docs for v0.6.2