coudenysj Jachim Coudenys on web technology


Controlling a receipt printer with ZeroMQ and PHP

When you create a POS system as a web app, you have the problem that not all the devices are controllable from your server.

In my case I have the following hardware connected to the workstation:

The input hardware is easy, because a barcode scanner is seen as a keyboard, and both devices can communicate to the server through the web browser.

The tricky part is when a sale is completed, and the customer wants a receipt.

Since the printer is connected to the (dummy) workstation and not to the (remote) server, we cannot print the receipt directly.

The solution I used for this problem is ZeroMQ.

On the server I have a PHP process running which binds to 2 ZeroMQ sockets. One (ZMQ::SOCKET_PULL) is waiting for incoming print request from the web app, one (ZMQ::SOCKET_PUB) is publishing a print request to all subscribing workstations.

On the workstation (in my case a Windows laptop with an HP receipt printer connected to it) I have another PHP process running which is waiting for print jobs (on a ZMQ::SOCKET_SUB socket).

I can add the final piece of the puzzle to the web app, by opening a ZMQ::SOCKET_PUSH socket and sending the URL of the receipt page.

Now I have created a lightweight system for controlling the receipt printer, instead of using cronjobs (or scheduled tasks) to check for print jobs.

Some remarks:

  • I could remove the print-dispatcher part, and let the web app connect to the print receiver directly, but I prefer to have a stable part (the binding sockets) on the known server (so both connecting sockets know the host to connect to).
  • The HTML page could be transported over ZeroMQ, but I like the extra request so the web app is sure the receipt is printed.
  • Curious Bear

    Quick question because I have been exploring the whole thermal receipt printer issue using PHP but never considered this approach. To be exact, I’ve been looking for the ability to trigger printing a receipt on a thermal printer from a web application (hosted locally).

    Does this command: exec(‘print /d:LPT2: ‘ . $file) actually allow you to print any arbitrary data on the printer? Or is this specific to this model of printer? I was under the (maybe wrong?) impression that thermal printers need to convert the print data into something specific so you can’t exactly throw HTML to it…

  • Hi,

    Because my printer (HP A799) only has drivers for windows, I had to use windows commands. The print command [] is actually a real windows command, and lets you print text to a printer port (some hacks [] are needed to talk to a USB printer, but even this is not too hard).

    I even found a printhtml.exe command [] where I can send basic HTML to a printer and the command will use IE to generate the page.

    I am now looking at a PDF solution [], because the printhtml command can be quite slow.

    I hope this answers your question.

  • Thanks for sharing! Now after reading this post, I came to know how to connect my digital printer with the remote serve without any hindrance.

  • July


    Would that work on an android tablet or an Ipad?