Print labels from Symfony
A Symfony service can post a label with HttpClient. Inject the API key from an env var. There is no Symfony bundle to enable.
For Symfony applications printing labels
Overview
Put the print call in a service, not in a controller that also renders HTML for the shopper. Inject HttpClientInterface and a printer code resolver. The key comes from %env(PRINTBASE_API_KEY)%.
Messenger can send the message after the doctrine transaction commits. The message carries the label id, not the API key.
ZPL or PDF is a choice of content_type inside that service. The controller should not know the byte layout.
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.
PHP
<?php
// Symfony HttpClient payload for a label PDF
$payload = [
'printer_code' => (int) getenv('PRINTER_CODE'),
'content_type' => 'pdf',
'content' => base64_encode($pdf),,
'copies' => 1,
];
$ch = curl_init('https://api.printbase.cloud/v1/print-jobs');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('PRINTBASE_API_KEY'),
'Content-Type: application/json',
'Idempotency-Key: ' . $labelId,
],
CURLOPT_POSTFIELDS => json_encode($payload),
]);
$body = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) >= 300) {
throw new RuntimeException($body);
}
$job = json_decode($body, true); // id, status=queued
Details for this setup
Env var, not parameters.yml committed to git
Symfony's secrets or the real environment. Rotate if it leaks into a log. The agent prints only what you send; it does not fetch the document from the platform for you.
HttpClient scopes
A scoped client for https://api.printbase.cloud keeps the base URL in one place.
Questions
Is there a PrintBase bundle?
No. The call is still POST /v1/print-jobs with a Bearer API key, and HTTP 201 means queued rather than printed.
How do I test?
Mock HttpClient in unit tests. In a staging environment, point at a staging printer_code.
Doctrine lifecycle?
Do not print in prePersist. The id may not exist, and the transaction may roll back. Dispatch after commit.
Raw TSPL? in this setup
content_type raw and the TSPL bytes. Symfony does not need to understand them.
Free plan: 100 jobs a month on one computer, no card.