Wasm and Cloudflare Workers
Compile the engine to wasm32 and render PDFs inside a JavaScript host without a file system.
With the wasm feature the engine compiles to wasm32-unknown-unknown and exports two
functions through wasm-bindgen. The Wasm engine reads no system fonts and no packs from disk;
it renders with the bundled Fira Sans.
Build
cargo build --release --target wasm32-unknown-unknown --no-default-features --features wasm
wasm-bindgen target/wasm32-unknown-unknown/release/renderreport.wasm --out-dir pkg --target web
--no-default-features leaves out barcodes, which a Worker rarely needs. Add it back with
--features wasm,barcodes if your reports use the barcode component.
To page images in the browser, add the png feature (--features wasm,png) for render_page_png. The
module is large because Typst and the bundled fonts are inside it: about 28 MiB raw and 11 MiB
gzip-compressed, so load it lazily and let the server compress it.
Playground
The playground on this site runs that build: edit a request, check it with validate
and see the pages rendered by render_page_png. site/scripts/build-wasm.sh builds the module for it
(the deploy workflow runs the script); the module loads only when you press Render.
render
Takes a render request as a JSON string and returns the PDF
bytes. Errors are thrown as strings (invalid RenderRequest JSON: …, render failed: …).
import { initSync, render } from './pkg/renderreport.js';
import wasmModule from './pkg/renderreport_bg.wasm'; // bundler-provided WebAssembly.Module
initSync({ module: wasmModule });
const pdf = render(JSON.stringify(request)); // Uint8Array
render_with_data
Takes a request template and a data object, both as JSON strings, binds the data into the
template (see templates and data) and returns the PDF bytes. Binding
errors are thrown as binding data failed: <template path>: <reason>.
const pdf = render_with_data(JSON.stringify(template), JSON.stringify(data));
render_wordsearch
Builds a complete word search document from a small request: an explanation page, the puzzle
and the solution, each on its own page. The grid is generated by the same word-search
component as everywhere else.
import { render_wordsearch } from './pkg/renderreport.js';
const pdf = render_wordsearch(JSON.stringify({
title: 'Animals',
words: ['CAT', 'DOG', 'GUINEA PIG'],
translations: ['Katze', 'Hund', 'Meerschweinchen'],
language: 'en',
}));
| Field | Default | Notes |
|---|---|---|
title |
– | Required |
words |
– | Required; spaces are kept in the word list, not in the grid |
description |
per language | Line above the grid |
translations |
[] |
One entry per word, null to skip; turns the word list into a vocabulary table |
width, height |
12 |
Grid size in letters |
allow_diagonal |
true |
|
allow_reverse |
false |
Words may also read right to left and bottom to top |
seed |
42 |
Same seed, same grid |
columns_word_list |
3 |
Columns of the plain word list |
language |
de |
de, en, fr or es; labels and explanation text |