The thermal receipt printer I am currently using doesn't natively supporting printing QR codes.

We can extend the escpos-php library to add image-based QR code support through the PhpQrCode library, as indicated in this tutorial by mike42 (the author of escpos-php).

QR code

These instructions assume a LAMP server with escpos-php has been configured per previous guides.

Install GD and PhpQrCode

Install the GD graphics library for PHP (verify your PHP version, alter GD library name as appropriate)

php --version
sudo apt-get install php7.2-gd
sudo service apache2 restart

Download the PhpQrCode library
Unzip it into /var/www/_phpqrcode

Subclass the escpos-php Printer class

We can create a new QrImgPrinter class which extends the escpos-php Printer class, but implements the qrCode function using image-based techniques rather than native printer support.

This code is based on the mike42 tutorial, but is updated to fix a few bugs and work with the current versions of PhpQrCode and escpos-php.

Create a new PHP file /var/www/_escpos/QrImgPrinter.php:

<?php
require_once ('/var/www/_escpos/vendor/autoload.php');
use Mike42\Escpos\Printer;
use Mike42\Escpos\EscposImage;

require_once ('/var/www/_phpqrcode/qrlib.php');


class QrImgPrinter extends Printer {

 function qrCode (string $content, int $ec = self::QR_ECLEVEL_L,
  int $size = 3, int $model = self::QR_MODEL_2)
 {
  // Validate inputs
  self::validateInteger ($ec, 0, 3, __FUNCTION__);
  self::validateInteger ($size, 1, 16, __FUNCTION__);
  self::validateInteger ($model, 1, 3, __FUNCTION__);
  if (strlen ($content) < 1)
    return;

  // Only Model 2 supported in phpqrcode
  $model = self::QR_MODEL_2;

  // Generate filename for temp file
  $tmpfname = tempnam (sys_get_temp_dir(), 'escpos-php');

  // Create QR code in temp file and print it
  QRcode::png ($content, $tmpfname, $ec, $size, 0, false);
  $img = EscposImage::load ($tmpfname, false);
  $this->bitImage ($img);

  // Delete temp file
  unlink ($tmpfname);
 }
}
?>

Use the QrImgPrinter class to print QR codes

In your existing receipt printing code, include the QrImgPrinter.php file and change your class instantiation from new Printer to new QrImgPrinter. Then call the qrCode member function normally to print a QR code as an image:

$connector = new FilePrintConnector ('php://stdout');
$printer = new QrImgPrinter ($connector);
$printer->initialize();
$printer->qrCode ('http://screaming.computer', Printer::QR_ECLEVEL_L, 6);
$printer->text ("\n"); // Flush
$printer->close();