go-typst/examples/passing-values/main.go
David Vogel 1aa13dbfb5
Some checks failed
test / test non-docker (1.23.x, 0.12.0) (push) Failing after 4s
test / test non-docker (1.23.x, 0.13.0) (push) Failing after 3s
test / test non-docker (1.23.x, 0.13.1) (push) Failing after 4s
test / test non-docker (1.23.x, 0.14.0) (push) Failing after 3s
test / test docker (1.23.x, ) (push) Failing after 23s
test / test docker (1.23.x, ghcr.io/typst/typst:0.14.0) (push) Failing after 15s
test / test docker (1.23.x, ghcr.io/typst/typst:v0.12.0) (push) Failing after 20s
test / test docker (1.23.x, ghcr.io/typst/typst:v0.13.0) (push) Failing after 19s
test / test docker (1.23.x, ghcr.io/typst/typst:v0.13.1) (push) Failing after 21s
Update README.md & Cleanup
2025-11-16 18:15:25 +00:00

54 lines
1.8 KiB
Go

package main
import (
"bytes"
"log"
"os"
"time"
"github.com/Dadido3/go-typst"
)
// DataEntry contains data to be passed to Typst.
type DataEntry struct {
Name string
Size struct{ X, Y, Z float64 }
Created time.Time
Numbers []int
}
var TestData = []DataEntry{
{Name: "Bell", Size: struct{ X, Y, Z float64 }{80, 40, 40}, Created: time.Date(2010, 12, 1, 12, 13, 14, 0, time.UTC), Numbers: []int{1, 2, 3}},
{Name: "Scissor", Size: struct{ X, Y, Z float64 }{200, 30, 10}, Created: time.Date(2015, 5, 12, 23, 5, 10, 0, time.UTC), Numbers: []int{4, 5, 10, 15}},
{Name: "Calculator", Size: struct{ X, Y, Z float64 }{150, 80, 8}, Created: time.Date(2016, 6, 10, 12, 15, 0, 0, time.UTC), Numbers: []int{16, 20, 30}},
{Name: "Key", Size: struct{ X, Y, Z float64 }{25, 10, 2}, Created: time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC), Numbers: []int{100, 199, 205}},
}
func main() {
var markup bytes.Buffer
// Inject Go values as Typst markup.
if err := typst.InjectValues(&markup, map[string]any{"data": TestData, "customText": "This data is coming from a Go application."}); err != nil {
log.Panicf("Failed to inject values into Typst markup: %v.", err)
}
// Import the template and invoke the template function with the custom data.
// Show is used to replace the current document with whatever content the template function in `template.typ` returns.
markup.WriteString(`
#import "template.typ": template
#show: doc => template(data, customText)`)
// Compile the prepared markup with Typst and write the result it into `output.pdf`.
f, err := os.Create("output.pdf")
if err != nil {
log.Panicf("Failed to create output file: %v.", err)
}
defer f.Close()
typstCaller := typst.CLI{}
if err := typstCaller.Compile(&markup, f, nil); err != nil {
log.Panicf("Failed to compile document: %v.", err)
}
}