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。請見處理錯誤。