printmelo-laravel maintained by printmelo
Printmelo for Laravel
Print from your Laravel app to printers on location with Printmelo. This package wraps the Printmelo PHP SDK with a facade, config, webhook events and Http::fake() support.
Requirements
- PHP 8.2 or newer
- Laravel 12 or 13
- A Printmelo API key. Create one in the dashboard under API keys.
Installation
composer require printmelo/printmelo-laravel
Add your key to .env:
PRINTMELO_API_KEY=pm_live_...
Check the connection:
php artisan printmelo:check
It shows the account, the scopes of the key and your printers with their ids.
To change more settings, publish the config:
php artisan vendor:publish --tag=printmelo-config
Printing
use Printmelo\Laravel\Facades\Printmelo;
$job = Printmelo::printJobs()->printPdfUrl(
printerId: 12,
url: route('orders.label', $order),
title: "Shipping label {$order->number}",
idempotencyKey: "order-{$order->number}-label",
);
Or inject the client:
use Printmelo\Printmelo;
class PrintShippingLabel
{
public function __construct(private Printmelo $printmelo) {}
public function handle(Order $order): void
{
$this->printmelo->printJobs->printRaw(
$order->warehouse->label_printer_id,
$order->zpl_label,
"Label {$order->number}",
idempotencyKey: "order-{$order->number}-label",
);
}
}
Always send an idempotency key, such as the order number with the kind of document. A second request with the same key returns the existing job instead of printing again, and the SDK can then safely retry on a network error. That makes printing from a queued job safe as well.
Everything the SDK can do works through the facade. Services are methods on the facade (Printmelo::printers()) and properties on the client ($printmelo->printers):
Printmelo::whoami();
Printmelo::printers()->list();
Printmelo::printers()->practice(); // test without paper
Printmelo::printJobs()->printPdfFile(12, storage_path('app/invoice.pdf'));
Printmelo::printJobs()->get($id);
Printmelo::printJobs()->cancel($id);
Printmelo::computers()->list();
Printmelo::pairingCodes()->create();
See the PHP SDK README for every method, the print options and the errors.
Integrators
Work on behalf of a child account by its id or your own reference:
Printmelo::forChildReference($customer->number)
->printJobs
->printPdfUrl($printerId, $url, 'Label');
Webhooks
The package registers POST /printmelo/webhook. It checks the signature, skips messages it already handled and fires Laravel events.
-
Create the webhook in the Printmelo dashboard under Webhooks, or in code:
$webhook = Printmelo::webhooks()->create(route('printmelo.webhook'), ['printjob.done', 'printjob.error']); -
Add the secret to
.env. You only see it once.PRINTMELO_WEBHOOK_SECRET=... -
Listen to the events:
use Printmelo\Laravel\Events\PrintJobDone; use Printmelo\Laravel\Events\PrintJobFailed; Event::listen(function (PrintJobDone $event) { Shipment::where('printmelo_job_id', $event->printJob->id)->update(['label_printed_at' => now()]); }); Event::listen(function (PrintJobFailed $event) { Log::warning('Label failed', ['job' => $event->printJob->id, 'reason' => $event->printJob->state_message]); });
| Event | Printmelo event |
|---|---|
PrintJobPrinting |
printjob.printing |
PrintJobWaiting |
printjob.waiting (out of paper, paused, offline) |
PrintJobDone |
printjob.done |
PrintJobFailed |
printjob.error |
PrintJobExpired |
printjob.expired |
PrintJobCancelled |
printjob.cancelled |
ComputerConnected |
computer.connected |
ComputerDisconnected |
computer.disconnected |
WebhookReceived |
every message, fired first |
Print job events have $event->printJob, computer events $event->computer, and all have the raw $event->event.
Printmelo waits at most 10 seconds for an answer. Make slow listeners queued with ShouldQueue. When a listener throws, the message is not marked as handled, so Printmelo's retry fires the events again.
The route has no CSRF check: it does not use the web middleware group. To use your own route, set PRINTMELO_WEBHOOK_PATH=null and add the printmelo.webhook middleware:
Route::post('hooks/printmelo', MyWebhookController::class)->middleware('printmelo.webhook');
// In the controller:
$event = $request->attributes->get('printmelo_event'); // Printmelo\Webhooks\WebhookEvent
Testing
Requests go through the Laravel HTTP client, so Http::fake() works:
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
Http::fake([
'api.printmelo.com/*' => Http::response(['data' => ['id' => 1, 'state' => 'new']], 201),
]);
// ... run your code ...
Http::assertSent(fn (Request $request) => $request->url() === 'https://api.printmelo.com/api/v1/printjobs'
&& $request['printer_id'] === 12);
Test your webhook listeners with a signed request:
use Printmelo\Webhooks\WebhookSignature;
$body = json_encode(['id' => (string) Str::uuid(), 'event' => 'printjob.done', 'created_at' => now()->toIso8601String(), 'data' => ['id' => 9, 'state' => 'done']]);
$this->call('POST', '/printmelo/webhook', server: [
'CONTENT_TYPE' => 'application/json',
'HTTP_X_PRINTMELO_SIGNATURE' => WebhookSignature::sign($body, config('printmelo.webhook.secret')),
], content: $body)->assertOk();
Or point the app at the practice printer to run real jobs without paper.
Configuration
| Variable | Default | |
|---|---|---|
PRINTMELO_API_KEY |
Required | |
PRINTMELO_BASE_URL |
https://api.printmelo.com/api/v1 |
|
PRINTMELO_TIMEOUT |
30 |
Seconds |
PRINTMELO_CONNECT_TIMEOUT |
10 |
Seconds |
PRINTMELO_MAX_RETRIES |
2 |
Reads, and print jobs with an idempotency key |
PRINTMELO_LOCALE |
app locale | Language of error messages: en or nl |
PRINTMELO_WEBHOOK_SECRET |
Required for webhooks | |
PRINTMELO_WEBHOOK_PATH |
printmelo/webhook |
null to register your own route |
PRINTMELO_WEBHOOK_TOLERANCE |
300 |
Seconds a message stays valid |
Development
composer install
composer test
License
MIT. See LICENSE.