PrintBase
  • Product
  • Pricing
  • Docs
  • Resources
PrintBase/专题/打印排障

小票中文乱码和 GBK

Agent 不会把 UTF-8 转成 GBK。中文小票要在 base64 之前按打印机字库编码。任务 completed 仍可能是乱码。调用 POST /v1/print-jobs,API Key 只放在服务端。

面向 小票中文变成乱码的开发

说明

raw 字节会被原样送进打印机。你把 UTF-8 中文放进 ESC/POS,打印机按自己的代码页解释,纸上就是乱码。任务状态仍然可以是 completed。打印机由门店电脑上的 Agent 主动连出,不需要公网 IP,也不需要把 9100 端口映射到互联网。

国内小票机常见字库是 GBK。请在 base64 之前完成转码,并按手册在初始化后打开汉字模式。JSON 外壳是 UTF-8,这不影响里面的字节。

若载荷以 %PDF- 开头却标成 raw,会直接 INVALID_CONTENT,那不是乱码,是类型用错。

创建任务使用 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

    先打一行英文

    英文正常说明链路是通的。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

  2. 02

    按 GBK 编码中文

    再做标准 base64。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

  3. 03

    加上打印机要求的汉字指令

    没有这步,字库不会启用。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

  4. 04

    核对列宽

    58 毫米约 32 列,80 毫米约 48 列。中文按双字节占位。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

Node.js

// 已经按打印机编码好的小票字节
const res = await fetch('https://api.printbase.cloud/v1/print-jobs', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.PRINTBASE_API_KEY}`,
    'Content-Type': 'application/json',
    'Idempotency-Key': orderId,
  },
  body: JSON.stringify({
    printer_code: Number(process.env.PRINTER_CODE),
    content_type: 'raw',
    content: Buffer.from(receipt).toString('base64'),
    copies: 1,
  }),
});
if (!res.ok) throw new Error(`PrintBase ${res.status}: ${await res.text()}`);
const job = await res.json();
// { id: "job_abc", status: "queued" }

这一套的细节

不要指望接口转码

没有这个参数。Agent 只打印你提交的内容,不会替你向平台拉取单据。Agent 只打印你提交的内容,不会替你向平台拉取单据。Agent 只打印你提交的内容,不会替你向平台拉取单据。

PDF 中文

走 content_type pdf 时,中文由 PDF 字体负责,和 ESC/POS 字库无关。Agent 只打印你提交的内容,不会替你向平台拉取单据。Agent 只打印你提交的内容,不会替你向平台拉取单据。

常见问题

JSON 已经是 UTF-8,为什么还要 GBK?

UTF-8 只是外层。小票机读的是你 base64 解码后的字节。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

部分汉字是空白?(详见本页)

字库里没有那个字,或编码错了一半。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

英文小票正常?(详见本页)

只能说明 ASCII 没问题。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

要不要改成 PDF?(详见本页)

只有在你打算用驱动打印整页时才改。切纸指令就不再由你逐字节控制。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

相关页面

  • 佳博 GP-58 远程打印 ESC/POS 小票

    GP-58 是 58 毫米小票机。发送 ESC/POS 原始字节,中文用 GBK。一行大约 32 个英文字宽。调用 POST /v1/print-jobs,API Key 只放在服务端。

  • 芯烨 XP-58IIH 远程打印 ESC/POS 小票

    XP-58IIH 是常见的 58 毫米 USB 小票机。用 ESC/POS raw 发送,中文 GBK,切纸前走纸。调用 POST /v1/print-jobs,API Key 只放在服务端。

  • 接口返回成功但没有出纸

    创建任务返回 201 只表示已排队。要看任务是否变成 completed。免费套餐用轮询,Webhook 在付费套餐。调用 POST /v1/print-jobs,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