Troubleshooting ESC/POS on MacOS

Writing code for an ESC/POS printer is not complicated, specially if you use a library that provides a higher level access like printAt, carriageReturn, cut, etc. But, sometimes to get the print just right might take some iterations and practicing with a real printer might not be as practical, or possible.

The following steps will help setup a virtual printer that can be used without having to have access to a physical printer or waste too much paper.

Requirements:

  • XQuartz or any other X11 system for MacOS
  • Docker for MacOS
  • Git

Start XQuartz (or the X11 system) and open up to receive connections

# inside the shell of XQuartz 
xhost + ${hostname}

# from a mac shell
# clone this escpos simulator
git clone https://github.com/dacduong/escpos-printer-simulator.git
cd espos-printer-simulator
# now run it in docker
sudo docker run --rm -it -v=$(pwd):/app -e DISPLAY=192.168.65.2:0 -p 9100:9100 java bash -c '/usr/bin/java -jar /app/binary/ESCPosPrinterSimulator.jar 9100'

Voilá… a virtual printer window should popup. Now, if you print to localhost:9100 (standard port for ESCPOS) you’ll see your results on the virtual printer. Good luck.