Problem
To send data on this route:
Client computer ---> Server (computer for Windows) ---> printer (dot matrix)
... and not allow Windows to work with data; instead, send raw data, including printer control codes, directly from the client computer.
My decision
Here's how I solved an almost identical problem for a small internal database application:
Step 1) Make access to the printer available without access to Windows, so that his fingers get into the data sent to him. I accomplished this by installing the printer using the "Generic / Text Only" driver, then installing RawPrintServer on a Windows machine connected to the printer.
Step 2) Send the raw data over the network to the TCP / IP port specified when setting up RawPrintServer (9100 by default). There are various ways to do this, here is what I did:
data = b"\ x1B@A String To Print\ x1B@ "
Background
I thought about the problem in two parts:
- Client machine: spills out the data I need with Python with the correct formatting / control codes for my printer and sends them over the network.
- Print Server Machine: Transferring Data to a Locally Connected Printer
Number 1 is the easy part. There are actually a few libraries in PyPI that can help with all printer codes, but I found that most of them were aimed at selling label printers, and were limited for me. So I just encoded what I needed into my Python program.
Of course, the way you decide to solve number 2 will affect how you send data with Python. I chose the TCP / IP route to avoid printing problems with Samba and Windows.
As you probably discovered, Windows usually very hard converts everything you want to print into a bitmap and start the printer in graphical mode. We can use a generic driver and upload data directly to the (local) printer port to prevent this.
There is no link from the network to the local printer port on the computer connected to the printer. Again, there are various ways to solve this problem. You can somehow try to access the Windows printer resource. If you go over TCP / IP, like me, you can write your own print server in Python. In my case, the RawPrintServer program "just worked", so I did not investigate it further. Obviously, all he does is capture incoming data from TCP port 9100 and drag it to the local printer port. Obviously, you need to be sure that the firewall is not blocking incoming connections on the print server machine. This method does not require the printer to be "shared" for Windows.
Depending on your situation (if you use DHCP), you may need additional work to get the server IP address in Python. In my case, I got IP for free due to the features of my application.
This solution seems to work very well for me. I have an old Panasonic printer working in Epson ESC / P compatibility mode, connected to a Windows 7 machine, which I can print from any other computer on the local network. By the way, this general idea should work regardless of which OS the client computer is running on.