PrintBase
  • Product
  • Pricing
  • Docs
  • Resources
PrintBase/专题/按语言接入远程打印

用 Go 远程打印 ZPL

Go 标准库 net/http 发送 ZPL。不需要 cgo,也不需要在容器里装斑马驱动。与英文 Go ZPL 页对应。调用 POST /v1/print-jobs,API Key 只放在服务端。

面向 用 Go 写打印服务的后端

其他语言版本: English

说明

encoding/base64 和 net/http 足够。ZPL 作为字符串编码成 UTF-8 再 base64。容器里不要装打印机驱动。打印机由门店电脑上的 Agent 主动连出,不需要公网 IP,也不需要把 9100 端口映射到互联网。

printer_code 用 JSON 数字。超时用 context,几秒即可。重试必须复用 Idempotency-Key。

这是英文 go-zpl 的中文页。站点列出了 Go SDK,此处写出的是稳定的 HTTP 形状。

创建任务使用 POST /v1/print-jobs,请求头 Authorization: Bearer,正文包含 printer_code、content_type(pdf 或 raw)以及 content 或 content_url 二者之一。免费套餐是一台电脑每月 100 个任务。

创建任务使用 POST /v1/print-jobs,请求头 Authorization: Bearer,正文包含 printer_code、content_type(pdf 或 raw)以及 content 或 content_url 二者之一。免费套餐是一台电脑每月 100 个任务。

接入步骤

  1. 01

    密钥放在服务端

    使用 pb_live_ 开头的 API Key,请求头 Authorization: Bearer。不要放进前端包或手机 App。

  2. 02

    打印机旁安装 Agent

    支持 Windows、macOS、Linux。GET /v1/printers 里的 code 就是 printer_code。

  3. 03

    调用 POST /v1/print-jobs

    content_type 只能是 pdf 或 raw。content 和 content_url 只能有一个。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

  4. 04

    重试用同一个幂等键

    换新键会再打一张。同一个键配不同 body 会返回 409。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

  5. 05

    查询任务再告诉用户

    GET /v1/print-jobs/{id}。付费套餐才有 print_job.completed 和 print_job.failed 的 Webhook。

Go

// Go ZPL
body, _ := json.Marshal(map[string]any{
    "printer_code": printerCode,
    "content_type": "raw",
    "content": base64.StdEncoding.EncodeToString([]byte(zpl)),,
    "copies": 1,
})
req, _ := http.NewRequest(http.MethodPost, "https://api.printbase.cloud/v1/print-jobs", bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+os.Getenv("PRINTBASE_API_KEY"))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Idempotency-Key", idempotencyKey)
res, err := http.DefaultClient.Do(req)
if err != nil { log.Fatal(err) }
defer res.Body.Close()
// 201 {"id":"job_abc","status":"queued"}

这一套的细节

不要把 API Key 打进日志

只记录任务 id 和状态码。Agent 只打印你提交的内容,不会替你向平台拉取单据。Agent 只打印你提交的内容,不会替你向平台拉取单据。Agent 只打印你提交的内容,不会替你向平台拉取单据。

context 超时

避免一次拨号挂住整个请求。Agent 只打印你提交的内容,不会替你向平台拉取单据。Agent 只打印你提交的内容,不会替你向平台拉取单据。Agent 只打印你提交的内容,不会替你向平台拉取单据。

常见问题

有官方 Go SDK 吗?

站点列出了。本页不假设具体的函数名,直接按文档组 JSON。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

cgo 链接 CUPS?

远程打印不需要。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

^PQ 谁来写?(详见本页)

简单的单段标签在 copies 大于 1 时,Agent 可能插入 ^PQ。你自己写了 ^PQ 就不会被改。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

PDF 用 Go 怎么打?

content_type 改为 pdf。英文页 go-pdf 是那个例子。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

相关页面

  • 用 Node.js 远程打印标签

    在 Node.js 里生成 ZPL 或 TSPL,以 raw 提交。与英文 Node ZPL 页对应。密钥放在服务端环境变量。调用 POST /v1/print-jobs,API Key 只放在服务端。

  • 斑马 ZD888 远程打印 ZPL 快递面单

    把 ZPL 发到斑马 ZD888。按机身配置页上的 dpi 计算点阵。Agent 打到系统队列,不需要打印机公网 IP。调用 POST /v1/print-jobs,API Key 只放在服务端。

  • 用 Spring Boot 打印小票

    在 Spring Boot 服务里用 RestClient 把 ESC/POS 小票 POST 到打印接口,由门店电脑上的 Agent 打到本地小票机。API Key 放在配置里,不作为请求参数。

免费套餐:一台电脑每月 100 个任务,不需要信用卡。

免费注册查看 API 文档
PrintBase

PrintBase connects your applications to physical printers anywhere - securely, reliably, and in real time.

Product
Receipt PrintingLabel PrintingDashboardDevicesWebhooks
Resources
DocsBlogStatusAPI ReferenceDownloadPrintBase vs PrintNodePrintNode PricingFAQ
Guides
All guidesIntegrationsPrintersCode samplesComparisonsUse casesTroubleshooting
Company
ContactSecurityPrivacy

© 2026 Aveniq Technologies OÜ. All rights reserved. PrintBase is a service of Aveniq Technologies OÜ.

PrivacyTermsRefunds