10

I need to build a simple web based printer server that will print a file to any given printers IP address

Using lp or lpr how can I print a file directly to a network printer by IP address? NOTE: The printer will NOT be setup in CUPS locally as it needs to have the ability to print to any IP address thrown at it.

What I have tried:

lp -d 10.11.234.75 /path/to/file
lpr -P 10.11.234.75 /path/to/file

Both give this: 'The printer or class does not exist.'

5 Answers 5

25

Try this:

cat you_file.prn | netcat -w 1 printer_ip 9100
0
5

If using bash then:

cat /path/to/file > /dev/tcp/10.11.234.75/9100
4
  • This is a really handy trick for testing if there is a network connection to the printer with direct output to the printer. echo 'test test test' > /dev/tcp/10.11.12.13/9100 Shame it only works from Bash and not from inside a Java program. Sep 18, 2020 at 12:32
  • This works great. But, how would I scale a PDF or adjust the margins? It always seems to cut off the very last line for me. This happens in GUI print dialogues as well, but usually I can select 90% scale there to work around it... which makes everything look smaller, but at least it works. How would I address it with this command? Jan 22, 2021 at 23:07
  • If the file is correct and made with the proper page size then maybe the printer needs to be reconfigured. Printers have a lot of settings. Maybe you could start with a new question instead of an old comment. Jan 24, 2021 at 1:05
  • @KatAmsterdam You should be able to treat /dev/tcp/10.11.12.13/9100 like a file and use the usual methods to write to it. After all, a bash > also just pipes the output of the previous command into the given file.
    – Turun
    Mar 24 at 18:15
3

What you want to do is probably not feasible. If the printers at the ends of these IP addresses are just random printers, then the server you're building would need to know which driver to use to be able to print to them. If you haven't installed them in any way beforehand then it's not going to work.

If you only want to talk to other Internet Printing Protocol (IPP) servers then it is possible, although not necessarily elegant. I don't know of any other Linux implementations of an IPP client than CUPS, and CUPS requires you to install printers in advance. This can be done very easily though (as explained here). It's the same code to add a normal printer (but you need to know which driver to use) as for an IPP server. Alternatively, you might be able to find another IPP implementation (or write one - it should be fairly simple just to send a document) which doesn't require installing printers.

Here's the code to add an IPP printer to CUPS:

lpadmin -E -p <printer-name> -v http://<ip_address>:631/<dir>/<printer> -L <location> -E

<printer-name> and <location> can be whatever you like, and you need the full network path to the printer.

To add a normal printer:

lpadmin -E -p <printer-name> -v <device-uri> -m <model> -L <location> -E

This is the same, except that you need to give a <model>, which is the driver for the printer. Scrap the first -E if you don't want encryption.

If you want to delete the printer afterwards, use this:

lpadmin -x <printer-name>
1

I found an old program called tcpsend.c to send a file to a printer at an IP address. Build with gcc -o tcpsend tcpsend.c

$ ./tcpsend
use: tcpsend [-t timeout] host port [files]
  -t timeout  - try connecting for timeout seconds

tcpsend.c source code

0

I had success using lp with a hostname and port.

echo foobar | lp -h 10.10.13.37:9100 -

Without specifying a port, i would get

lp: Error - No default destination


If printing a PDF, you can first convert it to PostScript using pdf2ps

pdf2ps file.pdf - | lp -h 10.10.13.37:9100 -

The argument - is used as an alias for standard input or output, letting us pipe the output of postscript straight into standard input of lp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.