Navigation

Go

Create images and PDFs in Go

Generate an image or a PDF from a template with the Go SDK.

Both methods take a context.Context and a template id. The examples use the ptr helper from the overview.

Create an image

CreateImage renders the template and returns the image bytes in Result. It takes a *CreateImageOptions, and nil uses the defaults.

#TemplateIDstringrequired

The template id, for example tpl_xxx.

#Modifications[]Modification

The values to put into the template’s layers. Each entry names a layer with Name. Layers you leave out keep the values from the template. Modifications lists the fields each layer accepts.

#Formatstringdefault: png

The image format: png, jpeg, or webp.

result := client.CreateImage(ctx, "tpl_xxx", &bannerify.CreateImageOptions{
	Modifications: []bannerify.Modification{
		{Name: "headline", Text: ptr("Launch promo")},
		{Name: "price", Text: ptr("$29")},
	},
	Format: "webp",
})
if result.Error != nil {
	return fmt.Errorf("create image: %s", result.Error.Message)
}

return os.WriteFile("launch-promo.webp", result.Result, 0o644)

Create a PDF

CreatePDF takes the modifications directly. It has no format option.

result := client.CreatePDF(ctx, "tpl_invoice", []bannerify.Modification{
	{Name: "customer", Text: ptr("Acme Studio")},
	{
		Name:    "line-items",
		Columns: ptr([]string{"Item", "Qty", "Total"}),
		Rows: ptr([]interface{}{
			map[string]interface{}{"Item": "Automate plan", "Qty": 1, "Total": "$29.00"},
		}),
	},
})
if result.Error != nil {
	return fmt.Errorf("create pdf: %s", result.Error.Message)
}

return os.WriteFile("invoice.pdf", result.Result, 0o644)

Modification fields

Modification has these fields. All of them except Name are pointers.

Field Type Layer
Name string Names the layer to change
Text *string Text
Src *string Image
Color *string Color
Visible *bool Shows or hides the layer
Qrcode *string QR code
Barcode *string Barcode
Star *float32 Star rating
Columns *[]string Table columns
Rows *[]interface{} Table, chart, or key value rows

The struct has no Title or Date field, so you cannot set an alert or a signature layer with it. Call the REST API for those layers.

Handle the result

Both methods return a *Response. See Handle errors.