How to use a Raspberry Pi as a print server

This post is designed for people who want to share a simple USB printer, such as this receipt printer, over the network.

Usually, you just connect up the printer to the computer like this:

2015-04-rpi-printer1

But if you are sending the print jobs from a central server, you would instead follow these steps, and hook up a Raspberry Pi near the printer to pass on the print-outs for you:

2015-04-rpi-printer2

This post will show you a very fuss-free way to do this. Because of its simplicity, if you have multiple computers printing (read: you need a server that can spool), or need two-way communication with the printer, then this setup will not be sufficient for your use case.

One-off setup

If your printer is /dev/usb/lp0, then the command to run is:

nohup nc -klp 9100 > /dev/usb/lp0 2> /dev/null&

There is quite a lot going on in this command, so I’m going to break it down into parts and explain what each one does.

nohup
Lets the command keep running after you log-out.
nc -klp 9100
Listens on port 9100 (-lp), and returns to listening after each connection (-k)
> /dev/usb/lp0
Redirects any incoming data to the printer device
2> /dev/null
Suppresses errors by sending them to /dev/null
&
Runs the command in the background so that you can keep using the terminal.

Run every boot

Simply schedule the command in cron as a @reboot task.

crontab -e

And add the line:

@reboot nohup nc -klp 9100 > /dev/usb/lp0 2> /dev/null&

Note that if you reboot the printer, you will also need to reboot the raspberry pi to get it to reconnect without logging in!

Send some tests

From a computer somewhere else on the network, send a test print-out:

echo "Hello world" | nc 10.x.x.x 9100

If the target printer is a thermal receipt printer, then you could also use escpos-php to send it more elaborate commands:

<?php
$fp = fsockopen("10.x.x.x", 9100);
/* Print a "Hello world" receipt" */
$printer = new Escpos($fp);
$printer -> text("Hello World!\n");
$printer -> cut();
fclose($fp);

7 Replies to “How to use a Raspberry Pi as a print server”

  1. Mike,

    I am trying to access the Raspbery Pi print server from my windows 10 box — using your example rather than through CUPS. What changes need to be made from the sending test scenario?

    Thanks!

  2. I have attached a Epson TM-T88V printer to the Raspberry PI and am able to test that the printer is working fine with python-escpos. I would like the POS software running on Windows 10 to connect to the Raspberry Pi without going through CUPS to print receipts. I have tested that installing CUPS on raspberry and installing a print driver added a an printer works — POS is able to print through printer connected to Raspberry. Is this possible? My main use case is to intercept the print stream and add a dynamic barcode at the bottom of the printed receipt. Should I do this through CUPS by adding a custom filter? Any insights to this problem is greatly appreciated.

  3. Hi Mike,
    I using my raspberry as print server connect to tm u220. print text working great. the problem is when i print your barcode sample, it just print empty line . and when i sue your example bit-image, receipt-with logo, the logo print in ascii code. i have installed php5 ,php5-gd.

  4. Hi mike, i have an update, i try print bit-image example using zonerich ab320 working fine, still fail using tm220 just print ascii code.
    thank you

  5. Tank you very much! This works. Exactly what I was looking for, no cups needed.

Leave a Reply

Your email address will not be published. Required fields are marked *