rawbt-receipt.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. require __DIR__ . '/../autoload.php';
  3. use Mike42\Escpos\Printer;
  4. use Mike42\Escpos\EscposImage;
  5. use Mike42\Escpos\PrintConnectors\RawbtPrintConnector;
  6. use Mike42\Escpos\CapabilityProfile;
  7. try {
  8. $profile = CapabilityProfile::load("POS-5890");
  9. /* Fill in your own connector here */
  10. $connector = new RawbtPrintConnector();
  11. /* Information for the receipt */
  12. $items = array(
  13. new item("Example item #1", "4.00"),
  14. new item("Another thing", "3.50"),
  15. new item("Something else", "1.00"),
  16. new item("A final item", "4.45"),
  17. );
  18. $subtotal = new item('Subtotal', '12.95');
  19. $tax = new item('A local tax', '1.30');
  20. $total = new item('Total', '14.25', true);
  21. /* Date is kept the same for testing */
  22. // $date = date('l jS \of F Y h:i:s A');
  23. $date = "Monday 6th of April 2015 02:56:25 PM";
  24. /* Start the printer */
  25. $logo = EscposImage::load("resources/rawbtlogo.png", false);
  26. $printer = new Printer($connector, $profile);
  27. /* Print top logo */
  28. if ($profile->getSupportsGraphics()) {
  29. $printer->graphics($logo);
  30. }
  31. if ($profile->getSupportsBitImageRaster() && !$profile->getSupportsGraphics()) {
  32. $printer->bitImage($logo);
  33. }
  34. /* Name of shop */
  35. $printer->setJustification(Printer::JUSTIFY_CENTER);
  36. $printer->selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
  37. $printer->text("ExampleMart Ltd.\n");
  38. $printer->selectPrintMode();
  39. $printer->text("Shop No. 42.\n");
  40. $printer->feed();
  41. /* Title of receipt */
  42. $printer->setEmphasis(true);
  43. $printer->text("SALES INVOICE\n");
  44. $printer->setEmphasis(false);
  45. /* Items */
  46. $printer->setJustification(Printer::JUSTIFY_LEFT);
  47. $printer->setEmphasis(true);
  48. $printer->text(new item('', '$'));
  49. $printer->setEmphasis(false);
  50. foreach ($items as $item) {
  51. $printer->text($item->getAsString(32)); // for 58mm Font A
  52. }
  53. $printer->setEmphasis(true);
  54. $printer->text($subtotal->getAsString(32));
  55. $printer->setEmphasis(false);
  56. $printer->feed();
  57. /* Tax and total */
  58. $printer->text($tax->getAsString(32));
  59. $printer->selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
  60. $printer->text($total->getAsString(32));
  61. $printer->selectPrintMode();
  62. /* Footer */
  63. $printer->feed(2);
  64. $printer->setJustification(Printer::JUSTIFY_CENTER);
  65. $printer->text("Thank you for shopping\n");
  66. $printer->text("at ExampleMart\n");
  67. $printer->text("For trading hours,\n");
  68. $printer->text("please visit example.com\n");
  69. $printer->feed(2);
  70. $printer->text($date . "\n");
  71. /* Barcode Default look */
  72. $printer->barcode("ABC", Printer::BARCODE_CODE39);
  73. $printer->feed();
  74. $printer->feed();
  75. // Demo that alignment QRcode is the same as text
  76. $printer2 = new Printer($connector); // dirty printer profile hack !!
  77. $printer2->setJustification(Printer::JUSTIFY_CENTER);
  78. $printer2->qrCode("https://rawbt.ru/mike42", Printer::QR_ECLEVEL_M, 8);
  79. $printer2->text("rawbt.ru/mike42\n");
  80. $printer2->setJustification();
  81. $printer2->feed();
  82. /* Cut the receipt and open the cash drawer */
  83. $printer->cut();
  84. $printer->pulse();
  85. } catch (Exception $e) {
  86. echo $e->getMessage();
  87. } finally {
  88. $printer->close();
  89. }
  90. /* A wrapper to do organise item names & prices into columns */
  91. class item
  92. {
  93. private $name;
  94. private $price;
  95. private $dollarSign;
  96. public function __construct($name = '', $price = '', $dollarSign = false)
  97. {
  98. $this->name = $name;
  99. $this->price = $price;
  100. $this->dollarSign = $dollarSign;
  101. }
  102. public function getAsString($width = 48)
  103. {
  104. $rightCols = 10;
  105. $leftCols = $width - $rightCols;
  106. if ($this->dollarSign) {
  107. $leftCols = $leftCols / 2 - $rightCols / 2;
  108. }
  109. $left = str_pad($this->name, $leftCols);
  110. $sign = ($this->dollarSign ? '$ ' : '');
  111. $right = str_pad($sign . $this->price, $rightCols, ' ', STR_PAD_LEFT);
  112. return "$left$right\n";
  113. }
  114. public function __toString()
  115. {
  116. return $this->getAsString();
  117. }
  118. }