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.
escpos-php library is fairly easy, however it uses the
composer dependency manager which I was previously unfamiliar with.
sudo apt install composer
sudo apt-get install php-intl
sudo service apache2 restart
Create a folder for
escpos-php in your web directory, then install it:
composer require mike42/escpos-php
Create a PHP file
printerTest.php in a suitable location:
$connector = new FilePrintConnector ('php://stdout');
$printer = new Printer ($connector);
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.