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

用 Spring Boot 打印小票

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

面向 用 Spring Boot 写订单服务的后端

其他语言版本: English

说明

在 @Service 里拼 ESC/POS 字节并用 RestClient 提交。控制器只负责权限。API Key 用配置或环境变量注入,不要出现在前端可调用的参数里。打印机由门店电脑上的 Agent 主动连出,不需要公网 IP,也不需要把 9100 端口映射到互联网。

订单事务提交之后再打,避免回滚了还出纸。printer_code 放在门店表。content_type 用 raw。中文在进 JSON 之前就按打印机编码放进字节数组。

这是英文 spring-boot-receipts 页的中文对应页,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。

Java

// Spring Boot 小票
var json = """
{"printer_code":%d,"content_type":"raw",\"content\":\"" + receiptBase64 + "\","copies":1}
""".formatted(printerCode);
var req = HttpRequest.newBuilder(URI.create("https://api.printbase.cloud/v1/print-jobs"))
    .header("Authorization", "Bearer " + apiKey)
    .header("Content-Type", "application/json")
    .header("Idempotency-Key", idempotencyKey)
    .POST(HttpRequest.BodyPublishers.ofString(json))
    .build();
var res = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());
if (res.statusCode() >= 300) throw new IllegalStateException(res.body());
// {"id":"job_abc","status":"queued"}

这一套的细节

事务提交之后

用事务事件在提交成功后再调用 HTTP,不要在回滚路径上出纸。Agent 只打印你提交的内容,不会替你向平台拉取单据。Agent 只打印你提交的内容,不会替你向平台拉取单据。

不要把密钥写进 application.yml 再提交到仓库

用环境变量或密钥管理。Agent 只打印你提交的内容,不会替你向平台拉取单据。Agent 只打印你提交的内容,不会替你向平台拉取单据。Agent 只打印你提交的内容,不会替你向平台拉取单据。

常见问题

有 Spring Starter 吗?

本产品没有单独的 starter。RestClient 加三个请求头即可。站点介绍里的 Java SDK 如果使用,也是打到同一路径。

WebClient 可以吗?

可以。JSON 字段不变。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

和本地 Java Print Service 的差别?

Print Service 只能看见运行 JVM 那台机器上的打印机。云上的 JVM 要用这个 HTTP 接口。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

小票乱码?(详见本页)(详见本页)

JSON 是 UTF-8,但 content 里的字节是你 base64 之前的编码。中文小票通常要 GBK。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

相关页面

  • 用 PHP 远程打印小票

    PHP 用 curl 发送 ESC/POS。网站服务器不连接打印机。与英文 PHP 小票页是同一接口。调用 POST /v1/print-jobs,API Key 只放在服务端。

  • 爱普生 TM-T82 远程打印小票

    爱普生 TM-T82 是国内常见的 80 毫米 ESC/POS 机。按原始字节发送,中文编码跟随机具设置。调用 POST /v1/print-jobs,API Key 只放在服务端。

  • 自建 PHP 商城如何自动打印订单小票

    在 PHP 订单支付逻辑里调用打印接口,把门店小票机打出来。不需要商城插件市场里的打印组件。调用 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