demo.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * This is a demo script for the functions of the PHP ESC/POS print driver,
  4. * Escpos.php.
  5. *
  6. * Most printers implement only a subset of the functionality of the driver, so
  7. * will not render this output correctly in all cases.
  8. *
  9. * @author Michael Billington <michael.billington@gmail.com>
  10. */
  11. require __DIR__ . '/../vendor/autoload.php';
  12. use Mike42\Escpos\Printer;
  13. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  14. use Mike42\Escpos\EscposImage;
  15. $connector = new FilePrintConnector("php://stdout");
  16. $printer = new Printer($connector);
  17. /* Initialize */
  18. $printer -> initialize();
  19. /* Text */
  20. $printer -> text("Hello world\n");
  21. $printer -> cut();
  22. /* Line feeds */
  23. $printer -> text("ABC");
  24. $printer -> feed(7);
  25. $printer -> text("DEF");
  26. $printer -> feedReverse(3);
  27. $printer -> text("GHI");
  28. $printer -> feed();
  29. $printer -> cut();
  30. /* Font modes */
  31. $modes = array(
  32. Printer::MODE_FONT_B,
  33. Printer::MODE_EMPHASIZED,
  34. Printer::MODE_DOUBLE_HEIGHT,
  35. Printer::MODE_DOUBLE_WIDTH,
  36. Printer::MODE_UNDERLINE);
  37. for ($i = 0; $i < pow(2, count($modes)); $i++) {
  38. $bits = str_pad(decbin($i), count($modes), "0", STR_PAD_LEFT);
  39. $mode = 0;
  40. for ($j = 0; $j < strlen($bits); $j++) {
  41. if (substr($bits, $j, 1) == "1") {
  42. $mode |= $modes[$j];
  43. }
  44. }
  45. $printer -> selectPrintMode($mode);
  46. $printer -> text("ABCDEFGHIJabcdefghijk\n");
  47. }
  48. $printer -> selectPrintMode(); // Reset
  49. $printer -> cut();
  50. /* Underline */
  51. for ($i = 0; $i < 3; $i++) {
  52. $printer -> setUnderline($i);
  53. $printer -> text("The quick brown fox jumps over the lazy dog\n");
  54. }
  55. $printer -> setUnderline(0); // Reset
  56. $printer -> cut();
  57. /* Cuts */
  58. $printer -> text("Partial cut\n(not available on all printers)\n");
  59. $printer -> cut(Printer::CUT_PARTIAL);
  60. $printer -> text("Full cut\n");
  61. $printer -> cut(Printer::CUT_FULL);
  62. /* Emphasis */
  63. for ($i = 0; $i < 2; $i++) {
  64. $printer -> setEmphasis($i == 1);
  65. $printer -> text("The quick brown fox jumps over the lazy dog\n");
  66. }
  67. $printer -> setEmphasis(false); // Reset
  68. $printer -> cut();
  69. /* Double-strike (looks basically the same as emphasis) */
  70. for ($i = 0; $i < 2; $i++) {
  71. $printer -> setDoubleStrike($i == 1);
  72. $printer -> text("The quick brown fox jumps over the lazy dog\n");
  73. }
  74. $printer -> setDoubleStrike(false);
  75. $printer -> cut();
  76. /* Fonts (many printers do not have a 'Font C') */
  77. $fonts = array(
  78. Printer::FONT_A,
  79. Printer::FONT_B,
  80. Printer::FONT_C);
  81. for ($i = 0; $i < count($fonts); $i++) {
  82. $printer -> setFont($fonts[$i]);
  83. $printer -> text("The quick brown fox jumps over the lazy dog\n");
  84. }
  85. $printer -> setFont(); // Reset
  86. $printer -> cut();
  87. /* Justification */
  88. $justification = array(
  89. Printer::JUSTIFY_LEFT,
  90. Printer::JUSTIFY_CENTER,
  91. Printer::JUSTIFY_RIGHT);
  92. for ($i = 0; $i < count($justification); $i++) {
  93. $printer -> setJustification($justification[$i]);
  94. $printer -> text("A man a plan a canal panama\n");
  95. }
  96. $printer -> setJustification(); // Reset
  97. $printer -> cut();
  98. /* Barcodes - see barcode.php for more detail */
  99. $printer -> setBarcodeHeight(80);
  100. $printer->setBarcodeTextPosition(Printer::BARCODE_TEXT_BELOW);
  101. $printer -> barcode("9876");
  102. $printer -> feed();
  103. $printer -> cut();
  104. /* Graphics - this demo will not work on some non-Epson printers */
  105. try {
  106. $logo = EscposImage::load("resources/escpos-php.png", false);
  107. $imgModes = array(
  108. Printer::IMG_DEFAULT,
  109. Printer::IMG_DOUBLE_WIDTH,
  110. Printer::IMG_DOUBLE_HEIGHT,
  111. Printer::IMG_DOUBLE_WIDTH | Printer::IMG_DOUBLE_HEIGHT
  112. );
  113. foreach ($imgModes as $mode) {
  114. $printer -> graphics($logo, $mode);
  115. }
  116. } catch (Exception $e) {
  117. /* Images not supported on your PHP, or image file not found */
  118. $printer -> text($e -> getMessage() . "\n");
  119. }
  120. $printer -> cut();
  121. /* Bit image */
  122. try {
  123. $logo = EscposImage::load("resources/escpos-php.png", false);
  124. $imgModes = array(
  125. Printer::IMG_DEFAULT,
  126. Printer::IMG_DOUBLE_WIDTH,
  127. Printer::IMG_DOUBLE_HEIGHT,
  128. Printer::IMG_DOUBLE_WIDTH | Printer::IMG_DOUBLE_HEIGHT
  129. );
  130. foreach ($imgModes as $mode) {
  131. $printer -> bitImage($logo, $mode);
  132. }
  133. } catch (Exception $e) {
  134. /* Images not supported on your PHP, or image file not found */
  135. $printer -> text($e -> getMessage() . "\n");
  136. }
  137. $printer -> cut();
  138. /* QR Code - see also the more in-depth demo at qr-code.php */
  139. $testStr = "Testing 123";
  140. $models = array(
  141. Printer::QR_MODEL_1 => "QR Model 1",
  142. Printer::QR_MODEL_2 => "QR Model 2 (default)",
  143. Printer::QR_MICRO => "Micro QR code\n(not supported on all printers)");
  144. foreach ($models as $model => $name) {
  145. $printer -> qrCode($testStr, Printer::QR_ECLEVEL_L, 3, $model);
  146. $printer -> text("$name\n");
  147. $printer -> feed();
  148. }
  149. $printer -> cut();
  150. /* Pulse */
  151. $printer -> pulse();
  152. /* Always close the printer! On some PrintConnectors, no actual
  153. * data is sent until the printer is closed. */
  154. $printer -> close();