Switch character table (code page) manually. Used in conjunction with textRaw() to
print special characters which can't be encoded automatically.
public selectCharacterTable ( integer $table ) | ||
$table | integer | The table to select. Available code tables are model-specific. |
/** * Encode a block of text using the specified map, and write it to the printer. * * @param string $text Text to print, UTF-8 format. * @param integer $encodingNo Encoding number to use- assumed to exist. */ private function writeTextUsingEncoding($text, $encodingNo) { $encodeMap = $this->encode[$encodingNo]; $len = mb_strlen($text, self::INPUT_ENCODING); $rawText = str_repeat(self::REPLACEMENT_CHAR, $len); $j = 0; for ($i = 0; $i < $len; $i++) { $char = mb_substr($text, $i, 1, self::INPUT_ENCODING); if (isset($encodeMap[$char])) { $rawText[$j] = $encodeMap[$char]; } elseif (self::asciiCheck($char)) { $rawText[$j] = $char; } elseif ($char === "\r") { /* Skip past Windows line endings (UTF-8 usage) */ continue; } $j++; } if ($this->printer->getCharacterTable() != $encodingNo) { $this->printer->selectCharacterTable($encodingNo); } $this->writeTextRaw(substr($rawText, 0, $j)); }
use Mike42\Escpos\Printer; use Mike42\Escpos\PrintConnectors\FilePrintConnector; use Mike42\Escpos\CapabilityProfiles\DefaultCapabilityProfile; // Enter connector and capability profile (to match your printer) $connector = new FilePrintConnector("php://stdout"); $profile = DefaultCapabilityProfile::getInstance(); $verbose = false; // Skip tables which iconv wont convert to (ie, only print characters available with UTF-8 input) /* Print a series of receipts containing i18n example strings - Code below shouldn't need changing */ $printer = new Mike42\Escpos\Printer($connector, $profile); $codePages = $profile->getCodePages(); $first = true; // Print larger table for first code-page. foreach ($codePages as $table => $page) { /* Change printer code page */ $printer->selectCharacterTable(255); $printer->selectCharacterTable($table); /* Select & print a label for it */ $label = $page->getId(); if (!$page->isEncodable()) { $label = " (not supported)"; } $printer->setEmphasis(true); $printer->textRaw("Table {$table}: {$label}\n"); $printer->setEmphasis(false); if (!$page->isEncodable() && !$verbose) { continue; // Skip non-recognised } /* Print a table of available characters (first table is larger than subsequent ones */ if ($first) {