Generate PDFs in the Browser with Rust, WASM, and Typst

77 points After-Smell6329 13 comments

Generating PDFs directly in the browser - without a server roundtrip - is no longer difficult to accomplish thanks to the awesome Typst library.

I read some articles in this sub about Typst and how good of an alternative it is to e.g. LateX and I stumbled upon the fact that it was WASM compatible. Since many users of web applications regularly need e.g. invoices or some business report printed to PDF I was curious how hard it would be to wrap the Typst library in an easy to use API which can be used in JavaScript.

=> [https://github.com/markonyango/pdf-generator\](https://github.com/markonyango/pdf-generator)

Provide a Typst template, provide input data for the purpose of rendering if you need to and click "Generate". Since you can turn anything into byte arrays and Typst has the handy "bytes" function to read in bytes, you can basically pass any data from the browser to the PDF (e.g. embed XML as meta data, emdbed files directly, etc.).

I have the feeling I left a lot unexplored (I only had a few days) and thus feel free to comment here or open Issues on the GitHub Repo.

P.S: The only downside so far was the size of the WASM module => ~17 MB is no joke :-/