A small go module to utilize Typst for PDF, SVG or PNG document/report generation.
Go to file
David Vogel 26b40a318d
Some checks failed
golangci-lint / lint (push) Successful in 34s
test / test (1.23.x, 0.12.0) (push) Failing after 4s
test / test (1.23.x, 0.13.0) (push) Failing after 3s
test / test (1.23.x, 0.13.1) (push) Failing after 3s
test / test (1.23.x, 0.14.0) (push) Failing after 3s
Fix empty struct and map being encoded as empty Typst array
2025-11-06 21:41:53 +01:00
.github Add compatibility with Typst 0.14.0 2025-10-24 22:01:41 +02:00
.vscode Add a way to change the working directory 2025-11-03 19:50:24 +01:00
documentation/images Update README.md 2025-01-12 14:57:32 +01:00
examples Rename Variable* to Value* 2025-02-27 18:07:46 +01:00
test-files Add ability to encode raw byte slices as Typst images 2025-11-04 23:17:17 +01:00
.gitignore Initial commit 2024-12-01 15:03:28 +01:00
cli_js.go Allow it to compile on unsupported platforms 2025-11-03 21:44:01 +01:00
cli_test.go Add a way to change the working directory 2025-11-03 19:50:24 +01:00
cli_unix.go Correct capitalization of Typst and Go 2025-02-27 18:23:30 +01:00
cli_wasi.go Allow it to compile on unsupported platforms 2025-11-03 21:44:01 +01:00
cli_windows.go Correct capitalization of Typst and Go 2025-02-27 18:23:30 +01:00
cli-options_test.go Add missing license headers 2025-11-03 19:48:05 +01:00
cli-options.go Add compatibility with Typst 0.14.0 2025-10-24 22:01:41 +02:00
cli.go Allow it to compile on unsupported platforms 2025-11-03 21:44:01 +01:00
errors_test.go Simplify and improve stderr parsing 2025-02-24 22:32:27 +01:00
errors.go Correct capitalization of Typst and Go 2025-02-27 18:23:30 +01:00
go.mod Several changes 2024-12-18 17:25:28 +01:00
go.sum Several changes 2024-12-18 17:25:28 +01:00
identifier_test.go Add MIT license 2024-12-18 20:48:49 +01:00
identifier.go Correct capitalization of Typst and Go 2025-02-27 18:23:30 +01:00
image_test.go Add ability to encode raw byte slices as Typst images 2025-11-04 23:17:17 +01:00
image.go Add ability to encode raw byte slices as Typst images 2025-11-04 23:17:17 +01:00
LICENSE Add MIT license 2024-12-18 20:48:49 +01:00
readme_test.go Add missing license headers 2025-11-03 19:48:05 +01:00
README.md Add ability to encode raw byte slices as Typst images 2025-11-04 23:17:17 +01:00
util_test.go Add missing license headers 2025-11-03 19:48:05 +01:00
util.go Correct capitalization of Typst and Go 2025-02-27 18:23:30 +01:00
value-encoder_test.go Fix empty struct and map being encoded as empty Typst array 2025-11-06 21:41:53 +01:00
value-encoder.go Fix empty struct and map being encoded as empty Typst array 2025-11-06 21:41:53 +01:00
variable-encoder-compat.go Rename Variable* to Value* 2025-02-27 18:07:46 +01:00

go-typst test

go-typst is a Go library that leverages the command-line interface of Typst to provide functions for the creation of documents and reports in various formats (PDF, SVG, PNG). Its goal is to provide Go developers with a seamless, "Go-like" interface to Typst's powerful document generation capabilities.

Stability and Compatibility

go-typst is a work in progress, and the API may change as Typst evolves. Supported Typst versions are tested by unit tests to ensure compatibility.

Supported Versions:

  • Typst 0.12.0
  • Typst 0.13.0
  • Typst 0.13.1
  • Typst 0.14.0

While breaking changes may occur, i aim to minimize disruptions. Use at your own discretion for production systems.

Features

  • PDF, SVG and PNG generation.
  • All Typst parameters are discoverable and documented in cli-options.go.
  • Go-to-Typst Value Encoder: Seamlessly inject any Go values.
  • Encode and inject images as a Typst markup simply by wrapping image.Image types or byte slices with raw JPEG or PNG data.
  • Errors from Typst CLI are returned as structured Go error objects with detailed information, such as line numbers and file paths.
  • Uses stdio; No temporary files will be created.
  • Good unit test coverage.

Installation

  1. Use go get github.com/Dadido3/go-typst inside of your project to add this module to your project.
  2. Install Typst by following the instructions in the Typst repository.

Runtime requirements

This module assumes that the Typst executable is accessible from your system's PATH. Ensure that you have Typst installed on any machine that your project will be executed. You can install it by following the instructions in the Typst repository.

Alternatively you can pack the Typst executable with your application. In this case you have to provide the path to the executable when setting up the typst.CLI object:

typstCLI := typst.CLI{
    ExecutablePath: "./typst", // Relative path to executable.
}

Note

Make sure to follow the Typst license requirements when you pack and distribute the Typst executable with your software.

Usage

Here we will create a simple PDF document by passing a reader with Typst markup into typstCLI.Compile and then let it write the resulting PDF data into a file:

func main() {
    r := bytes.NewBufferString(`#set page(width: 100mm, height: auto, margin: 5mm)
= go-typst

A library to generate documents and reports by utilizing the command line version of Typst.
#footnote[https://typst.app/]

== Features

- Encoder to convert Go objects into Typst objects which then can be injected into Typst documents.
- Parsing of returned errors into Go error objects.
- Uses stdio; No temporary files need to be created.
- Test coverage of most features.`)

    typstCLI := typst.CLI{}

    f, err := os.Create("output.pdf")
    if err != nil {
        t.Fatalf("Failed to create output file: %v.", err)
    }
    defer f.Close()

    if err := typstCLI.Compile(r, f, nil); err != nil {
        t.Fatalf("Failed to compile document: %v.", err)
    }
}

The resulting document will look like this:

readme-1.svg