character-tables.php 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * This demo prints out supported code pages on your printer. This is intended
  4. * for debugging character-encoding issues: If your printer does not work with
  5. * a built-in capability profile, you need to check its documentation for
  6. * supported code pages.
  7. *
  8. * These are then loaded into a capability profile, which maps code page
  9. * numbers to iconv encoding names on your particular printer. This script
  10. * will print all configured code pages, so that you can check that the chosen
  11. * iconv encoding name matches the actual code page contents.
  12. *
  13. * If this is correctly set up for your printer, then the driver will try its
  14. * best to map UTF-8 text into these code pages for you, allowing you to accept
  15. * arbitrary input from a database, without worrying about encoding it for the printer.
  16. */
  17. require __DIR__ . '/../vendor/autoload.php';
  18. use Mike42\Escpos\Printer;
  19. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  20. use Mike42\Escpos\CapabilityProfile;
  21. // Enter connector and capability profile (to match your printer)
  22. $connector = new FilePrintConnector("php://stdout");
  23. $profile = CapabilityProfile::load("default");
  24. $verbose = false; // Skip tables which iconv wont convert to (ie, only print characters available with UTF-8 input)
  25. /* Print a series of receipts containing i18n example strings - Code below shouldn't need changing */
  26. $printer = new Mike42\Escpos\Printer($connector, $profile);
  27. $codePages = $profile -> getCodePages();
  28. $first = true; // Print larger table for first code-page.
  29. foreach ($codePages as $table => $page) {
  30. /* Change printer code page */
  31. $printer -> selectCharacterTable(255);
  32. $printer -> selectCharacterTable($table);
  33. /* Select & print a label for it */
  34. $label = $page -> getId();
  35. if (!$page -> isEncodable()) {
  36. $label= " (not supported)";
  37. }
  38. $printer -> setEmphasis(true);
  39. $printer -> textRaw("Table $table: $label\n");
  40. $printer -> setEmphasis(false);
  41. if (!$page -> isEncodable() && !$verbose) {
  42. continue; // Skip non-recognised
  43. }
  44. /* Print a table of available characters (first table is larger than subsequent ones */
  45. if ($first) {
  46. $first = false;
  47. compactCharTable($printer, 1, true);
  48. } else {
  49. compactCharTable($printer);
  50. }
  51. }
  52. $printer -> cut();
  53. $printer -> close();
  54. function compactCharTable($printer, $start = 4, $header = false)
  55. {
  56. /* Output a compact character table for the current encoding */
  57. $chars = str_repeat(' ', 256);
  58. for ($i = 0; $i < 255; $i++) {
  59. $chars[$i] = ($i > 32 && $i != 127) ? chr($i) : ' ';
  60. }
  61. if ($header) {
  62. $printer -> setEmphasis(true);
  63. $printer -> textRaw(" 0123456789ABCDEF0123456789ABCDEF\n");
  64. $printer -> setEmphasis(false);
  65. }
  66. for ($y = $start; $y < 8; $y++) {
  67. $printer -> setEmphasis(true);
  68. $printer -> textRaw(strtoupper(dechex($y * 2)) . " ");
  69. $printer -> setEmphasis(false);
  70. $printer -> textRaw(substr($chars, $y * 32, 32) . "\n");
  71. }
  72. }