Print receipts from Spring Boot
Print ESC/POS receipts from a Spring Boot service: build the bytes in a @Service, post them with RestClient or WebClient, and keep the API key in a property, not a request field.
For Spring Boot order services
This page in another language: 中文
Overview
Inject the API key with @Value or a configuration property. A @Service method builds the ESC/POS bytes and posts them. Controllers stay thin. @Transactional methods should publish an event and print after commit.
RestClient is enough. WebClient is fine if the application is already reactive. Either way the body is printer_code, content_type raw, and base64 content.
This English page and the Chinese Spring page are the same task in two languages.
Create the job with POST /v1/print-jobs, an Authorization: Bearer header, printer_code, content_type pdf or raw, and exactly one of content or content_url. The free plan is 100 jobs a month on one computer.
Integration steps
- 01
Create an API key on the server
Use an API key (pb_live_…) in the process environment. Authorization is Bearer. Do not ship the key to browsers or mobile apps.
- 02
Install the agent beside the printer
Windows, macOS, or Linux. GET /v1/printers and keep the numeric code.
- 03
POST /v1/print-jobs
content_type is pdf or raw. Send exactly one of content (standard base64) or content_url.
- 04
Reuse the idempotency key on retry
A new key means a second physical print. HTTP 409 means the same key was reused with a different body.
- 05
Read the job before you tell the user
GET /v1/print-jobs/{id}. Paid plans can also receive print_job.completed and print_job.failed webhooks.
Java
// Spring Boot receipt
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"}
Details for this setup
After commit
TransactionalEventListener(phase = AFTER_COMMIT) avoids printing rolled-back orders.
Per-store printer
A Store entity can hold printerCode. Validate it before you call. The agent prints only what you send; it does not fetch the document from the platform for you.
Questions
Is there a Spring starter?
Not in this product. RestClient plus three headers is the integration.
Feign? in this setup
You can declare a client. The path is POST /v1/print-jobs. Do not invent extra fields.
Actuator health versus printer health?
Actuator does not know the printer. GET the job or the printer list if you need that.
Chinese receipts?
Encode GBK in the byte array. Spring's JSON converter only encodes the outer JSON, which is UTF-8.
Free plan: 100 jobs a month on one computer, no card.