If the API library only supports a network connection for printing and the printer is a device without network connector but with USB connector, then we have to be creative and create a configuration that handles the translation for us.
The machine I used for this purpose is a Linux server running Ubuntu 16.04 LTS. But this could also have been a Raspberry Pi for example.
The printer is an Epson TM-T88V. This is a Point of Sale POS / receipt printer.
HP JetDirect Protocol
A simple network protocol for print jobs that is still relevant today is JetDirect running on TCP port 9100. JetDirect was designed by HP in the early 1990’s.
So what we actually want to accomplish is Linux to act as a network printer device on port 9100 that forwards all of the data to the USB connected printer.
Determine the Printer’s USB Device
When the printer is connected to the USB port and switched on, a new USB device will appear under /dev/usb. The first USB device will most likely be assigned to /dev/usb/lp0. We can easily test the USB connection of the printer.
echo "Hi POS printer!" > /dev/usb/lp0
The JetDirect protocol is just an ordinary network stream and actually not a real protocol. So all we need is a ‘network stream server’ like e.g. xinetd to listen on port 9100 and redirect this stream to the printer stream.
if xinetd is not installed yet, we first need to install it using apt-get:
# apt-get install xinetd
Now add port 9100 to the file /etc/services if not already added:
jetdirect 9100/tcp laserjet hplj
To start listening on port 9100, create the file /etc/xinetd.d/jetdirect with the following contents:
# Allow applications using the AppSocket / JetDirect protocol
socket_type = stream
protocol = tcp
wait = no
user = lp
server = /usr/local/bin/jetdirect
groups = yes
disable = no
Now create the file /usr/local/bin/jetdirect, this bash script receives a stream from standard input and forwards it to device /dev/usb/lp0:
/bin/cat > /dev/usb/lp0
Restart xinetd to activate the new configuration:
# service xinetd restart
Test the Final Result
Everything should be up and running by now. To test the network printer connection execute the following command:
$ echo "Hi Network Printer!" | nc 127.0.0.1 9100
Linux as a network printer device (Raw, port 9100) – https://unix.stackexchange.com/questions/203173/linux-as-a-network-printer-device-raw-port-9100
Using port 9100 to print to a Printer Queue on a Cups-enabled Server – https://www.freedomit.co.nz/kb-centos/76-using-port-9100-to-print-to-a-printer-queue-on-a-cups-enabled-server