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?
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 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.