Print without a dialog in JavaScript
Browsers will not silently print from a public page. The workable pattern is your backend plus the print API, not a hidden window.print.
For Frontend developers hitting the print dialog
This page in another language: 中文
Overview
window.print() is defined to involve the user. A public page cannot open a USB device, and it cannot rely on calling http://127.0.0.1 to a printer helper. Chrome's private-network protections and certificate rules keep closing that door.
Call your backend with the order id. The backend holds the API key and POSTs /v1/print-jobs. The shop agent prints. The browser renders a job id. That is silent from the guest's point of view because no dialog appears on their laptop, and the paper is somewhere else.
The agent can listen on 127.0.0.1:17891, but the allow-list is localhost and printbase.cloud origins. Your production origin receives 403. Do not ship the API key to get around that.
Integration steps
- 01
Stop calling window.print for this job
Use it only when the user should pick a printer on their own computer.
- 02
Post the order id to your server
Not the API key, not the raw printer socket. Use POST /v1/print-jobs with Authorization: Bearer and the printer_code from GET /v1/printers.
- 03
Server submits the job
Bearer token, printer_code, pdf or raw. Use POST /v1/print-jobs with Authorization: Bearer and the printer_code from GET /v1/printers.
- 04
Poll status for the operator
The guest usually does not need the spooler detail. The kitchen display does.
Node.js
// Backend the browser is allowed to call
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': idempotencyKey,
},
body: JSON.stringify({
printer_code: Number(process.env.PRINTER_CODE),
content_type: 'pdf',
content: pdf.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" }
Details for this setup
Local port
Empty Origin is allowed. localhost, 127.0.0.1, ::1, and printbase.cloud hosts are allowed. Anything else is forbidden.
Kiosk exception
A page served from localhost on the kiosk PC can use the local port. A public kiosk URL should still use the cloud API.
Questions
Can an iframe hide the dialog?
No. The dialog is not a DOM element you can restyle away. The call is still POST /v1/print-jobs with a Bearer API key, and HTTP 201 means queued rather than printed.
What about QZ Tray?
It is a local bridge with its own signing. See the QZ pages. It is not window.print.
Will a Chrome flag fix this?
Do not depend on a user flipping a flag. The server path does not need one.
Is the PDF printed on the user's laptop?
Only if that laptop runs the agent and you targeted its printer. Usually you target the shop.
Free plan: 100 jobs a month on one computer, no card.