Printing guide
Print thermal labels from a web app: four ways
A web page cannot open a raw connection to a printer. Here is why, and the four routes that work, with what each one needs and where each one breaks.
Why a browser cannot just print a label
A page in a browser can make HTTP requests, open a WebSocket and use a handful of other web protocols. It cannot open a plain TCP connection to a printer's port 9100 and write bytes into it. The limit is deliberate: if any page you visit could open any connection on your network, every page would become a tool for attacking the devices on it.
The browser's print dialog does not solve this. It draws a page through the printer driver, while a label printer wants its own command text, such as ZPL, to arrive untouched. People searching for how to print to a Zebra printer from a web app usually run into exactly this gap.
A few browsers offer device APIs. Web Serial, for example, lets a page write to a serial port. MDN lists it as limited availability: it needs a secure context, the user must choose the device in a prompt, and it does not work in some widely used browsers. It also reaches serial devices, not network printers. Treat it as a niche option, not a plan for a fleet.
So the question is never how the page prints. It is who writes the bytes while the page waits. There are four candidates: your server, the printer itself, a small program on the user's computer, or an agent that collects jobs from a queue.
The four ways at a glance
| Way | Who writes to the printer | What it needs | Where it breaks |
|---|---|---|---|
| 1. Server to a LAN printer | Your server, over TCP, usually on port 9100. | A server that can reach the printer's address. | The server is in a data center and the printer is behind a firewall. |
| 2. Browser to the printer's web service | The browser, over HTTP, to a service the printer hosts. | A printer that accepts print data over HTTP, and a user on the same network. | Cross-origin rules, mixed content and local-network prompts. |
| 3. Local bridge | A small program on the workstation that listens locally and writes to the printer. | An install on every workstation, kept updated and running. | Phones, tablets and any computer you cannot install software on. |
| 4. Print queue and agent | An agent where the printers are, collecting jobs from a queue on a server you run. | A queue server, one agent per site, and outbound HTTPS allowed. | The agent is offline, so jobs wait. |
Way 1: your server writes to a LAN printer
The button in the web app sends a small request to your server: which label, which printer. The server builds the label, looks up the printer's address and writes the bytes over TCP, usually to port 9100. The browser never touches the printer. Send ZPL to a Zebra printer from C#
This is the cleanest route when you can use it, and the right one for an in-house system whose server sits on the same network as its printers. Store printers by name, not by address, and give each printer a reserved address so a router restart does not move it.
It breaks when the server is somewhere else. A server in a data center has no route to a printer in a warehouse, and opening the printer's port to the internet is not an option, because the raw port has no sign-in. If the server must stay remote, you need a VPN or a tunnel, or way 4.
BarcoderXThe SDK packages print from your own .NET process over TCP, serial, the Windows spooler, CUPS or USB. The details are in server-side label printing for .NET.
Way 2: the browser posts to the printer's built-in web service
Some network printers run a small web server of their own for setup and status, and some models accept print data through it. In theory the page could send the label straight to that address. In practice three browser rules get in the way.
- Cross-origin requests
- Your code can read the printer's answer only if the printer sends the right CORS headers, which you cannot assume. A request in no-cors mode can still go out, but the response is opaque: your code cannot tell success from failure.
- Mixed content
- If your app is served over HTTPS and the printer answers on plain HTTP, the browser blocks the request. Getting a certificate that browsers trust for a printer on a private address is hard, which leaves you stuck between the two.
- Local network permission
- Browsers that implement Local Network Access show the user a permission prompt before a public website can reach a local address, and only in a secure context. That is one more prompt on every browser and for every user.
That makes way 2 a fit for a pilot, a kiosk you configure yourself or a single site where you control the browser. It is a weak base for a product that has to work on whatever the customer already has.
Way 3: a small bridge program on the workstation
A bridge is a small program installed on the user's computer. It listens on a local address, the web page sends it the label, and the bridge writes the bytes to the printer over USB, the spooler or the network. Because the page talks to the same machine, the mixed-content rule is less of a problem than it is for a remote printer: MDN treats loopback addresses as secure origins.
Two costs decide whether this fits. First, you install, update and keep running one program on every workstation, and it cannot exist on a phone, a tablet or a locked-down computer. Second, the bridge is a door on that machine. Any web page could try to call it, so it has to check where a request comes from and refuse the rest. Browsers that implement Local Network Access can also ask the user for permission before a page reaches a loopback address.
It fits a fixed set of managed computers with USB printers. It fits badly anywhere else.
BarcoderXWith BarcoderX the browser never calls a port on your machine, and no bridge program is installed on the user's workstation. A web app built on BarcoderX sends the job to the API server you run, and the print agent, which makes outbound connections only, delivers it.
Way 4: a print queue and an outbound-only agent
The web app sends the job over HTTPS to a server you run, on your own hardware or in your own cloud account. That is all the browser ever does, so none of the printer-side browser rules apply. The server queues the job. An agent, installed once per site on a machine that can see the printers, keeps an outbound connection to your server, collects the job and writes it to the printer. Nobody has to open an inbound port.
This is the route that works from any device with a browser, phones and tablets included, and for printers you do not own or control. The costs are an agent to install and update at each site, a queue server you run and secure, outbound HTTPS that the network must allow, and a wait when the agent is offline. Because the job is a record with a state, you can show the user where it is. Return labels printed at a service desk, event labels printed at a desk and specimen labels printed at a draw station are jobs of this kind; a return label the customer prints at home is a PDF export instead.
BarcoderXThis is the route BarcoderX is built around, and both halves run on your infrastructure: the API server and the print agent. Your web app queues a job through the REST API (POST /print-jobs, relative to /api/v1). The agent runs as a Windows service or a systemd daemon, dials out over HTTPS and one WebSocket, and delivers the job. A job for an agent that is offline waits until its expiry.
How the print agent works The print agent: pairing, connection and jobs
Failure modes to plan for
- A silent success
- A no-cors request, or a socket write that returns, says nothing about paper. Read the printer's status where the transport allows it, and show the user the difference between sent and printed.
- The wrong printer
- Addresses change when a router hands out new ones. Store printers by name, reserve addresses, and let an administrator point a name at a new address.
- A blocked request
- Mixed content and permission prompts fail inside the browser, often with only a console message. Test over real HTTPS, not only on localhost, before you ship way 2 or way 3.
- A bridge that is not running
- A page cannot reach a program that is closed. Detect it and show the user how to start it.
- An offline agent
- Jobs wait. Decide how long they are worth keeping, set an expiry, and show queued jobs so that a person can act on them.
- Printer faults
- Out of media or ribbon, head open. Only some transports can see them. Through the spooler or CUPS you see the queue, not the printer.
Choosing: a decision list
- Way 1
Is your server on the printers' network? Use way 1.
- Way 4
Do users work on many networks you do not control, or on phones and tablets? Use way 4.
- Way 3
Is it a fixed set of managed computers with USB printers, where you can install software? Way 3 can work. Way 4 still avoids an install on every computer.
- Way 2
Is it a pilot, or one site where you control both the browser and the printer? Way 2 can work. Do not build a product on it.
- Any
Still unsure? Prefer the route where the browser talks only to your own service. Ways 1 and 4 do.
Keep reading
Server-side label printing for .NET Thermal label printing and export The REST API For developersNew to the printer language? Read the ZPL guides and command reference.
Background General technical background from MDN Web Docs: mixed content, CORS, request modes, the Web Serial API and Local Network Access. What this page says about BarcoderX comes from its printing, API, platform, developers and FAQ pages.
Written by BarcoderX in its own words.
Zebra, ZPL and ZPL II are trademarks of Zebra Technologies Corporation. BarcoderX is an independent product and is not affiliated with, endorsed by or sponsored by Zebra Technologies.