typstpdfgenerator

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2026 License: MIT Imports: 14 Imported by: 0

README

typst-pdf-generator-go

Go client for generating PDFs with typst-pdf-generator.

Usage

All generation methods accept a context.Context and return a ResponseInfo containing CorrelationID, Stdout, and Stderr. The correlation ID can be set by the caller via WithCorrelationID, and the client will prefer a correlation/request ID returned by the server when present. checkout the examples folder for more usage examples.

package main

import (
	"context"
	"log"
	"time"

	typstpdfgenerator "github.com/4Sigma/typst-pdf-generator-go"
)

func main() {
	client, err := typstpdfgenerator.New(
		"YOUR_AUTH_KEY",
		"https://YOUR_FAAS_GATEWAY/function/typst",
		typstpdfgenerator.WithTimeout(120*time.Second),
	)
	if err != nil {
		log.Fatal(err)
	}

	ctx := typstpdfgenerator.WithCorrelationID(context.Background(), "my-correlation-id")

	info, err := client.SavePDF(
		ctx,
		"",               // content
		"template.typ",   // template file
		"output.pdf",     // output file
		nil,              // options
		nil,              // media
	)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("generated PDF, correlation_id=%s", info.CorrelationID)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotGenerated   = errors.New("PDF not generated")
	ErrConnection     = errors.New("connection error")
	ErrInvalidAuth    = errors.New("auth key cannot be empty")
	ErrInvalidGateway = errors.New("FaaS gateway cannot be empty")
)

Functions

func CorrelationIDFromContext

func CorrelationIDFromContext(ctx context.Context) string

CorrelationIDFromContext extracts a correlation ID previously set with WithCorrelationID. Returns an empty string if none is set.

func WithCorrelationID

func WithCorrelationID(ctx context.Context, correlationID string) context.Context

WithCorrelationID returns a new context carrying the provided correlation ID.

If correlationID is empty, ctx is returned unchanged.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func New

func New(authKey, faasGateway string, opts ...Option) (*Client, error)

func (*Client) Convert

func (c *Client) Convert(ctx context.Context, w io.Writer, content string, templateData []byte, options []string, media []MediaFile) (ResponseInfo, error)

func (*Client) GeneratePDFFromFile

func (c *Client) GeneratePDFFromFile(ctx context.Context, w io.Writer, content, templateFilePath string, options []string, media []MediaFile) (ResponseInfo, error)

func (*Client) GeneratePDFFromString

func (c *Client) GeneratePDFFromString(ctx context.Context, w io.Writer, content, templateString string, options []string, media []MediaFile) (ResponseInfo, error)

func (*Client) SavePDF

func (c *Client) SavePDF(ctx context.Context, content, templateFilePath, outputPath string, options []string, media []MediaFile) (ResponseInfo, error)

type ConnectionError

type ConnectionError struct {
	Message string
	Err     error
}

func (*ConnectionError) Error

func (e *ConnectionError) Error() string

func (*ConnectionError) Unwrap

func (e *ConnectionError) Unwrap() error

type HTTPError

type HTTPError struct {
	StatusCode    int
	Status        string
	Body          string
	CorrelationID string
}

func (*HTTPError) Error

func (e *HTTPError) Error() string

func (*HTTPError) Unwrap

func (e *HTTPError) Unwrap() error

type MediaFile

type MediaFile struct {
	Name string
	Data []byte
}

type NotGeneratedError

type NotGeneratedError struct {
	Message       string
	CorrelationID string
}

func (*NotGeneratedError) Error

func (e *NotGeneratedError) Error() string

func (*NotGeneratedError) Unwrap

func (e *NotGeneratedError) Unwrap() error

type Option

type Option func(*Client) error

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

func WithInsecureSkipVerify

func WithInsecureSkipVerify() Option

func WithTimeout

func WithTimeout(timeout time.Duration) Option

type ResponseInfo

type ResponseInfo struct {
	Stdout        string
	Stderr        string
	CorrelationID string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL