Most thermal receipt printers are controlled using the ESC/POS protocol. This is a standard set of control codes to control font size and style, print images and barcodes, etc. (though not all printers support all features — some testing is generally required). The escpos-php library is a fantastic piece of software which wraps this protocol in an easy-to-use set of PHP classes.

Installing the escpos-php library is fairly easy, however it uses the composer dependency manager which I was previously unfamiliar with.

These are the steps I took to install escpos-php on my Linux server VM and print using my thermal receipt printer:

Install composer:

sudo apt install composer

Install the php-intl module:

sudo apt-get install php-intl
sudo service apache2 restart

Create a folder for escpos-php in your web directory, then install it:

mkdir /var/www/_escpos
cd /var/www/_escpos
composer require mike42/escpos-php

Create a PHP file printerTest.php in a suitable location:

require_once ('/var/www/_escpos/vendor/autoload.php');
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;

$connector = new FilePrintConnector ('php://stdout');
$printer = new Printer ($connector);

$printer->text ("test\n");

This PHP file will output the binary control codes to stdout. We run the program and pipe the output to our printer as follows:

php printerTest.php | lp -s -d THERMALPRINTER

In theory, we could use a CupsPrintConnector instead of the FilePrintConnector which would save us the step of redirecting the output. However, in my (very brief) test, the output seems to go into a print queue and must be manually released before it will print. This may have been a one-time hiccup or there may be a workaround, but using the FilePrintConnector avoided the issue and suits my needs for now.