character-encodings.php 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /* Change to the correct path if you copy this example! */
  3. require __DIR__ . '/../vendor/autoload.php';
  4. use Mike42\Escpos\Printer;
  5. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  6. use Mike42\Escpos\CapabilityProfile;
  7. /**
  8. * This demonstrates available character encodings. Escpos-php accepts UTF-8,
  9. * and converts this to lower-level data to the printer. This is a complex area, so be
  10. * prepared to code a model-specific hack ('CapabilityProfile') for your printer.
  11. *
  12. * If you run into trouble, please file an issue on GitHub, including at a minimum:
  13. * - A UTF-8 test string in the language you're working in, and
  14. * - A test print or link to a technical document which lists the available
  15. * code pages ('character code tables') for your printer.
  16. *
  17. * The DefaultCapabilityProfile works for Espson-branded printers. For other models, you
  18. * must use/create a PrinterCapabilityProfile for your printer containing a list of code
  19. * page numbers for your printer- otherwise you will get mojibake.
  20. *
  21. * If you do not intend to use non-English characters, then use SimpleCapabilityProfile,
  22. * which has only the default encoding, effectively disabling code page changes.
  23. */
  24. include(dirname(__FILE__) . '/resources/character-encoding-test-strings.inc');
  25. try {
  26. // Enter connector and capability profile (to match your printer)
  27. $connector = new FilePrintConnector("php://stdout");
  28. $profile = CapabilityProfile::load("default");
  29. /* Print a series of receipts containing i18n example strings */
  30. $printer = new Printer($connector, $profile);
  31. $printer -> selectPrintMode(Printer::MODE_DOUBLE_HEIGHT | Printer::MODE_EMPHASIZED | Printer::MODE_DOUBLE_WIDTH);
  32. $printer -> text("Implemented languages\n");
  33. $printer -> selectPrintMode();
  34. foreach ($inputsOk as $label => $str) {
  35. $printer -> setEmphasis(true);
  36. $printer -> text($label . ":\n");
  37. $printer -> setEmphasis(false);
  38. $printer -> text($str);
  39. }
  40. $printer -> feed();
  41. $printer -> selectPrintMode(Printer::MODE_DOUBLE_HEIGHT | Printer::MODE_EMPHASIZED | Printer::MODE_DOUBLE_WIDTH);
  42. $printer -> text("Works in progress\n");
  43. $printer -> selectPrintMode();
  44. foreach ($inputsNotOk as $label => $str) {
  45. $printer -> setEmphasis(true);
  46. $printer -> text($label . ":\n");
  47. $printer -> setEmphasis(false);
  48. $printer -> text($str);
  49. }
  50. $printer -> cut();
  51. /* Close printer */
  52. $printer -> close();
  53. } catch (Exception $e) {
  54. echo "Couldn't print to this printer: " . $e -> getMessage() . "\n";
  55. }