printing photos using ncat

I recently discovered this somewhat known and neat trick on linux where you can print any document straight up from the terminal without having to install proprietary printer drivers, CUPS or whatsoever - just by using `ncat`.

This is possible due to the fact that many networked printers expose the port 9100 which is used for raw TCP/IP printing also known as JetDirect or "raw" printing. This printing method works by opening a TCP socket connection to the port 9100 and streaming a print-ready file like a pdf or jpeg directly to the printer input buffer.

Here is how you can try it:

1. Install nmap

sudo pacman -S nmap

2. Find your printer ip address and check for port 9100

nmap -sn 192.168.1.0/24

3. Run the following command:

ncat <printer_ip> 9100 < file.pdf

You can even print plain text (using the default printer font)

echo "hello printer!" | ncat <printer_ip> 9100

I also tried to print a photo. Here is the photo I decided to test print:

original photo

Because of the fact that by using this printing we are essentially just sending raw bytes to the printer without providing specific printing instructins about the paper quality and size, it is necessary to somewhat prepare the file yourself. So in my case, I had to calculate the pixel density I wanted to print based on the size of the photo paper, crop the image to completely fill the available frame (avoid white space) and convert to jpeg if the file is not already a jpeg.

The process went as follows:

1. convert to jpeg and crop image file to photo paper dimensions using imagick

convert photo.jpg -resize 1181x1772^ -gravity center -extent 1181x1772 -density 300 -units PixelsPerInch photo_preview.jpg

2. print using

nc <printer_ip> 9100 < print_preview.jpg

The printed image looks stunning! Almost like a professional print.

printed photos on my wall

I suppose it's because printing using this way doesn't use any ink preservation/optimization method and therefore the printer is always doing its best(?) 

Anyways, that was definetely a fun experiment.

Further reading: