barcode.php 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. require __DIR__ . '/../vendor/autoload.php';
  3. use Mike42\Escpos\Printer;
  4. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  5. $connector = new FilePrintConnector("php://stdout");
  6. $printer = new Printer($connector);
  7. /* Height and width */
  8. $printer->selectPrintMode(Printer::MODE_DOUBLE_HEIGHT | Printer::MODE_DOUBLE_WIDTH);
  9. $printer->text("Height and bar width\n");
  10. $printer->selectPrintMode();
  11. $heights = array(1, 2, 4, 8, 16, 32);
  12. $widths = array(1, 2, 3, 4, 5, 6, 7, 8);
  13. $printer -> text("Default look\n");
  14. $printer->barcode("ABC", Printer::BARCODE_CODE39);
  15. foreach($heights as $height) {
  16. $printer -> text("\nHeight $height\n");
  17. $printer->setBarcodeHeight($height);
  18. $printer->barcode("ABC", Printer::BARCODE_CODE39);
  19. }
  20. foreach($widths as $width) {
  21. $printer -> text("\nWidth $width\n");
  22. $printer->setBarcodeWidth($width);
  23. $printer->barcode("ABC", Printer::BARCODE_CODE39);
  24. }
  25. $printer->feed();
  26. // Set to something sensible for the rest of the examples
  27. $printer->setBarcodeHeight(40);
  28. $printer->setBarcodeWidth(2);
  29. /* Text position */
  30. $printer->selectPrintMode(Printer::MODE_DOUBLE_HEIGHT | Printer::MODE_DOUBLE_WIDTH);
  31. $printer->text("Text position\n");
  32. $printer->selectPrintMode();
  33. $hri = array (
  34. Printer::BARCODE_TEXT_NONE => "No text",
  35. Printer::BARCODE_TEXT_ABOVE => "Above",
  36. Printer::BARCODE_TEXT_BELOW => "Below",
  37. Printer::BARCODE_TEXT_ABOVE | Printer::BARCODE_TEXT_BELOW => "Both"
  38. );
  39. foreach ($hri as $position => $caption) {
  40. $printer->text($caption . "\n");
  41. $printer->setBarcodeTextPosition($position);
  42. $printer->barcode("012345678901", Printer::BARCODE_JAN13);
  43. $printer->feed();
  44. }
  45. /* Barcode types */
  46. $standards = array (
  47. Printer::BARCODE_UPCA => array (
  48. "title" => "UPC-A",
  49. "caption" => "Fixed-length numeric product barcodes.",
  50. "example" => array (
  51. array (
  52. "caption" => "12 char numeric including (wrong) check digit.",
  53. "content" => "012345678901"
  54. ),
  55. array (
  56. "caption" => "Send 11 chars to add check digit automatically.",
  57. "content" => "01234567890"
  58. )
  59. )
  60. ),
  61. Printer::BARCODE_UPCE => array (
  62. "title" => "UPC-E",
  63. "caption" => "Fixed-length numeric compact product barcodes.",
  64. "example" => array (
  65. array (
  66. "caption" => "6 char numeric - auto check digit & NSC",
  67. "content" => "123456"
  68. ),
  69. array (
  70. "caption" => "7 char numeric - auto check digit",
  71. "content" => "0123456"
  72. ),
  73. array (
  74. "caption" => "8 char numeric",
  75. "content" => "01234567"
  76. ),
  77. array (
  78. "caption" => "11 char numeric - auto check digit",
  79. "content" => "01234567890"
  80. ),
  81. array (
  82. "caption" => "12 char numeric including (wrong) check digit",
  83. "content" => "012345678901"
  84. )
  85. )
  86. ),
  87. Printer::BARCODE_JAN13 => array (
  88. "title" => "JAN13/EAN13",
  89. "caption" => "Fixed-length numeric barcodes.",
  90. "example" => array (
  91. array (
  92. "caption" => "12 char numeric - auto check digit",
  93. "content" => "012345678901"
  94. ),
  95. array (
  96. "caption" => "13 char numeric including (wrong) check digit",
  97. "content" => "0123456789012"
  98. )
  99. )
  100. ),
  101. Printer::BARCODE_JAN8 => array (
  102. "title" => "JAN8/EAN8",
  103. "caption" => "Fixed-length numeric barcodes.",
  104. "example" => array (
  105. array (
  106. "caption" => "7 char numeric - auto check digit",
  107. "content" => "0123456"
  108. ),
  109. array (
  110. "caption" => "8 char numeric including (wrong) check digit",
  111. "content" => "01234567"
  112. )
  113. )
  114. ),
  115. Printer::BARCODE_CODE39 => array (
  116. "title" => "Code39",
  117. "caption" => "Variable length alphanumeric w/ some special chars.",
  118. "example" => array (
  119. array (
  120. "caption" => "Text, numbers, spaces",
  121. "content" => "ABC 012"
  122. ),
  123. array (
  124. "caption" => "Special characters",
  125. "content" => "$%+-./"
  126. ),
  127. array (
  128. "caption" => "Extra char (*) Used as start/stop",
  129. "content" => "*TEXT*"
  130. )
  131. )
  132. ),
  133. Printer::BARCODE_ITF => array (
  134. "title" => "ITF",
  135. "caption" => "Variable length numeric w/even number of digits,\nas they are encoded in pairs.",
  136. "example" => array (
  137. array (
  138. "caption" => "Numeric- even number of digits",
  139. "content" => "0123456789"
  140. )
  141. )
  142. ),
  143. Printer::BARCODE_CODABAR => array (
  144. "title" => "Codabar",
  145. "caption" => "Varaible length numeric with some allowable\nextra characters. ABCD/abcd must be used as\nstart/stop characters (one at the start, one\nat the end) to distinguish between barcode\napplications.",
  146. "example" => array (
  147. array (
  148. "caption" => "Numeric w/ A A start/stop. ",
  149. "content" => "A012345A"
  150. ),
  151. array (
  152. "caption" => "Extra allowable characters",
  153. "content" => "A012$+-./:A"
  154. )
  155. )
  156. ),
  157. Printer::BARCODE_CODE93 => array (
  158. "title" => "Code93",
  159. "caption" => "Variable length- any ASCII is available",
  160. "example" => array (
  161. array (
  162. "caption" => "Text",
  163. "content" => "012abcd"
  164. )
  165. )
  166. ),
  167. Printer::BARCODE_CODE128 => array (
  168. "title" => "Code128",
  169. "caption" => "Variable length- any ASCII is available",
  170. "example" => array (
  171. array (
  172. "caption" => "Code set A uppercase & symbols",
  173. "content" => "{A" . "012ABCD"
  174. ),
  175. array (
  176. "caption" => "Code set B general text",
  177. "content" => "{B" . "012ABCDabcd"
  178. ),
  179. array (
  180. "caption" => "Code set C compact numbers\n Sending chr(21) chr(32) chr(43)",
  181. "content" => "{C" . chr(21) . chr(32) . chr(43)
  182. )
  183. )
  184. )
  185. );
  186. $printer->setBarcodeTextPosition(Printer::BARCODE_TEXT_BELOW);
  187. foreach ($standards as $type => $standard) {
  188. $printer->selectPrintMode(Printer::MODE_DOUBLE_HEIGHT | Printer::MODE_DOUBLE_WIDTH);
  189. $printer->text($standard ["title"] . "\n");
  190. $printer->selectPrintMode();
  191. $printer->text($standard ["caption"] . "\n\n");
  192. foreach ($standard ["example"] as $id => $barcode) {
  193. $printer->setEmphasis(true);
  194. $printer->text($barcode ["caption"] . "\n");
  195. $printer->setEmphasis(false);
  196. $printer->text("Content: " . $barcode ["content"] . "\n");
  197. $printer->barcode($barcode ["content"], $type);
  198. $printer->feed();
  199. }
  200. }
  201. $printer->cut();
  202. $printer->close();