receipt-with-logo.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. require __DIR__ . '/../vendor/autoload.php';
  3. use Mike42\Escpos\Printer;
  4. use Mike42\Escpos\EscposImage;
  5. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  6. /* Fill in your own connector here */
  7. $connector = new FilePrintConnector("php://stdout");
  8. /* Information for the receipt */
  9. $items = array(
  10. new item("Example item #1", "4.00"),
  11. new item("Another thing", "3.50"),
  12. new item("Something else", "1.00"),
  13. new item("A final item", "4.45"),
  14. );
  15. $subtotal = new item('Subtotal', '12.95');
  16. $tax = new item('A local tax', '1.30');
  17. $total = new item('Total', '14.25', true);
  18. /* Date is kept the same for testing */
  19. // $date = date('l jS \of F Y h:i:s A');
  20. $date = "Monday 6th of April 2015 02:56:25 PM";
  21. /* Start the printer */
  22. $logo = EscposImage::load("resources/escpos-php.png", false);
  23. $printer = new Printer($connector);
  24. /* Print top logo */
  25. $printer -> setJustification(Printer::JUSTIFY_CENTER);
  26. $printer -> graphics($logo);
  27. /* Name of shop */
  28. $printer -> selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
  29. $printer -> text("ExampleMart Ltd.\n");
  30. $printer -> selectPrintMode();
  31. $printer -> text("Shop No. 42.\n");
  32. $printer -> feed();
  33. /* Title of receipt */
  34. $printer -> setEmphasis(true);
  35. $printer -> text("SALES INVOICE\n");
  36. $printer -> setEmphasis(false);
  37. /* Items */
  38. $printer -> setJustification(Printer::JUSTIFY_LEFT);
  39. $printer -> setEmphasis(true);
  40. $printer -> text(new item('', '$'));
  41. $printer -> setEmphasis(false);
  42. foreach ($items as $item) {
  43. $printer -> text($item);
  44. }
  45. $printer -> setEmphasis(true);
  46. $printer -> text($subtotal);
  47. $printer -> setEmphasis(false);
  48. $printer -> feed();
  49. /* Tax and total */
  50. $printer -> text($tax);
  51. $printer -> selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
  52. $printer -> text($total);
  53. $printer -> selectPrintMode();
  54. /* Footer */
  55. $printer -> feed(2);
  56. $printer -> setJustification(Printer::JUSTIFY_CENTER);
  57. $printer -> text("Thank you for shopping at ExampleMart\n");
  58. $printer -> text("For trading hours, please visit example.com\n");
  59. $printer -> feed(2);
  60. $printer -> text($date . "\n");
  61. /* Cut the receipt and open the cash drawer */
  62. $printer -> cut();
  63. $printer -> pulse();
  64. $printer -> close();
  65. /* A wrapper to do organise item names & prices into columns */
  66. class item
  67. {
  68. private $name;
  69. private $price;
  70. private $dollarSign;
  71. public function __construct($name = '', $price = '', $dollarSign = false)
  72. {
  73. $this -> name = $name;
  74. $this -> price = $price;
  75. $this -> dollarSign = $dollarSign;
  76. }
  77. public function __toString()
  78. {
  79. $rightCols = 10;
  80. $leftCols = 38;
  81. if ($this -> dollarSign) {
  82. $leftCols = $leftCols / 2 - $rightCols / 2;
  83. }
  84. $left = str_pad($this -> name, $leftCols) ;
  85. $sign = ($this -> dollarSign ? '$ ' : '');
  86. $right = str_pad($sign . $this -> price, $rightCols, ' ', STR_PAD_LEFT);
  87. return "$left$right\n";
  88. }
  89. }