A small go module to utilize Typst for PDF, SVG or PNG document/report generation.
Go to file
David Vogel ed5897c9f6 Fix typst syntax with VariableEncoder
- Arrays with a single entry need a trailing comma
- Add writeRune method
- Negative numbers need to be put in code brackets, otherwise the typst parser will complain in some cases
- Add/change unit tests
- Let TestVariableEncoder test compile generated markup
- Update README.md
2024-12-19 16:48:50 +01:00
.github Create FUNDING.yml 2024-12-19 00:20:31 +01:00
.vscode Add MIT license 2024-12-18 20:48:49 +01:00
documentation/images Update README.md & Add test for README.md code 2024-12-18 21:24:36 +01:00
examples/passing-objects Add example for passing go values 2024-12-18 23:20:12 +01:00
.gitignore Initial commit 2024-12-01 15:03:28 +01:00
cli_test.go Add VersionString method 2024-12-18 23:31:48 +01:00
cli_unix.go Add MIT license 2024-12-18 20:48:49 +01:00
cli_windows.go Add MIT license 2024-12-18 20:48:49 +01:00
cli-options.go Add MIT license 2024-12-18 20:48:49 +01:00
cli.go Switch back to --diagnostic-format human 2024-12-19 16:14:11 +01:00
errors_test.go Add MIT license 2024-12-18 20:48:49 +01:00
errors.go Switch back to --diagnostic-format human 2024-12-19 16:14:11 +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 Add MIT license 2024-12-18 20:48:49 +01:00
LICENSE Add MIT license 2024-12-18 20:48:49 +01:00
readme_test.go Update README.md & Add test for README.md code 2024-12-18 21:24:36 +01:00
README.md Fix typst syntax with VariableEncoder 2024-12-19 16:48:50 +01:00
variable-encoder_test.go Fix typst syntax with VariableEncoder 2024-12-19 16:48:50 +01:00
variable-encoder.go Fix typst syntax with VariableEncoder 2024-12-19 16:48:50 +01:00

go-typst test

A library to generate documents and reports by utilizing the command line version of typst.

This is basically a binding to typst-cli which exposes functions needed to compile documents into different formats like PDF, SVG or PNG. The goal is to make using typst as simple and "go like" as possible.

This module, along with typst itself, is a work in progress. The API may change, and compatibility with different typst versions are also not set in stone. There is no way to prevent this as long as typst has breaking changes. To mitigate problems arising from this, most of the functionality is unit tested against different typst releases. The supported and tested versions right now are:

  • Typst 0.12.0

Features

  • PDF, SVG and PNG generation.
  • All typst-cli parameters are available as a struct, which makes it easy to discover all available options.
  • Encoder to convert go values into typst markup which can be injected into typst documents.
  • Any stderr will be returned as go error value, including line number, column and file path of the error.
  • 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

You need to have typst installed on any machine that you want to run your go project on. You can install it by following the instructions in the typst repository.

Usage

Here we will create a simple PDF document by passing a reader with typst markup into typstCLI.Render 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.Render(r, f, nil); err != nil {
        t.Fatalf("Failed to render document: %v.", err)
    }
}

The resulting document will look like this:

readme-1.svg