Go
Go で画像と PDF を作成する
Go SDK で、テンプレートから画像または PDF を生成します。
どちらのメソッドも、context.Context とテンプレート ID を受け取ります。例では、概要の ptr ヘルパーを使っています。
画像を作成する
CreateImage はテンプレートをレンダリングし、画像のバイト列を Result に入れて返します。*CreateImageOptions を受け取り、nil を渡すと既定値を使います。
テンプレート ID。たとえば tpl_xxx です。
テンプレートのレイヤーに設定する値。各項目は Name でレイヤーを指定します。指定しなかったレイヤーは、テンプレートの値のままです。各レイヤーが受け付けるフィールドは、Modifications にあります。
画像の形式。png、jpeg、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)
PDF を作成する
CreatePDF は、modifications をそのまま受け取ります。形式のオプションはありません。
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 のフィールド
Modification には次のフィールドがあります。Name 以外はすべてポインターです。
| フィールド | 型 | レイヤー |
|---|---|---|
Name |
string |
変更するレイヤーの名前 |
Text |
*string |
テキスト |
Src |
*string |
画像 |
Color |
*string |
色 |
Visible |
*bool |
レイヤーの表示と非表示 |
Qrcode |
*string |
QR コード |
Barcode |
*string |
バーコード |
Star |
*float32 |
星の評価 |
Columns |
*[]string |
表の列 |
Rows |
*[]interface{} |
表、グラフ、キーと値の行 |
この構造体には Title と Date のフィールドがないため、アラートや署名のレイヤーは設定できません。これらのレイヤーには、REST API を使ってください。
結果を処理する
どちらのメソッドも *Response を返します。エラーを処理するを参照してください。