Connect any printer to your homepage

I recently had a very exciting client project where I’ve created a theater booking system and one part of the requirements was a connection to their ticket printer.

Cause it’s a client project I will not post some code in this article. It just describes my way throught this project, the ways I took, the reasons and what I’ve tried with or without success. When I reference any free library or documentation it’s linked.

A the beginning I had the advantage that the system is used in the webbrowser and the guy who sits next the entrance checks the reservations, prints the tickets using the website and hands it out to the visitor. Ok, that’s easy, let’s create an pdf or image which can be printed out. Unfortunately it’s not very usable because who visits a theater alone?

Another requirement was a maximum of 10 seats per reservation. So it is possible that our printing guy has to create 10 PDFs/images, opens each one, click on print (or press [shift]+[P]) and submit the printing dialog. Thats not very comfortable and will take a while.

So I’ve searched for a way to print multiple tickets automatically without getting forced to confirm this printing dialog. The computer systems used are all using Windows 7 and as printer they gave me a Boca Lemur.
Boca has plenty of drivers for all operating systems and very good documentation about their hardware. So I’ve worked myself through all of them and had a few plans to solve my problem.

The printer system is programmable using an easy to understand API and I’d just have to create a file describing the ticket content and send it to the printer using an serial adapter or USB. There can also example code be found in their documentation but It’s written in basic. I’m a bit of an Linux inthusiastic so I have limited advantages using Windows but I think basic is also in the Windows world not state of the art.

I’ve searched for a few other options to write data over a usb port to the printer, the serial port was not possible due to the used hardware at the theater.
My decision was java because I’ve written much more in Java (in special android) than in C/C++ and consords. But it was harder than I thought because I wasn’t able to list the available ports on my Windows8 system.

On the other side the printer setup worked perfect and after minutes I was able to print a windows testpage on the ticket printer. When it’s possible to print a testpage it must also be possible to print something else. So I’ve tried to print a normal textdocument with „foobar“ as content and it got also printed out. That was the point when I’ve changed my plans and I’ve found the Java printing API.

With this API it’s easy to send something to any installed printer. You can define a new class extending java.awt.print.Printable which implements a print method which gets an Graphics object.

This graphics object makes it possible to draw something on the ticket using java’s graphics API. It’s also possible to define a font and draw text on the area using left and top coordinates.

I’ve used lots of the provided ticket blanks to generate the perfect result but finally I made it. The next step was to think about how to send data from the homepage?
My answer was json over a http websocket connection. So the printing server gets started on client side (I’ve placed an icon showing the theaters logo in the windows taskbar to show the status or stop the server) and the homepage connects using javascripts websocket API.

FIrst I’ve made a few tries with a simple self written HTTP-Server and ajax polling requests on the browser site to communicate with the printing server. It also worked but there I had problems with javascripts same domain policy (sorted them out using the Access-Control-Allow-Origin header) and of course it’s not very usefull to poll the client machine every second when the printing server is not started. I’ve found a wonderful java websocket library at Github which performs the handshake itself and it was easy to create the websocket server.

As next step the homepage had to speak with the client. Using 127.0.0.1 and the port the printing server listens to I was soon able to create a successfull connection between them.

The last thing was to write the javascript client api which checks if the server is running and connects to it. If the connection is successfull the print button on the homepage appears and when our friend at the entrance clicks on it the javascript clients sends all relevant data as json string to the printing server running on the his computer.

The print server performs a few checks if all needed data for the ticket got sent and their values and responds with an error on which the client can react or create the ticket and send it to the printer.

The printer is really fast and prints 3 tickets per second. In my tests is was possible to send 10 different tickets throught the websocket and the printer threw all of them out without any delay.

Success 🙂

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.