141-custom-command.php 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. require __DIR__ . '/../../vendor/autoload.php';
  3. use Mike42\Escpos\Printer;
  4. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  5. /**
  6. * This example shows how to send a custom command to the printer
  7. *
  8. * "ESC ( B" is the barcode function for Epson LX300 series.
  9. * This is not part of standard ESC/POS, but it's a good example
  10. * of how to send some binary to the driver.
  11. */
  12. /* Barcode type is used in this script */
  13. const EAN13 = 0;
  14. /* Barcode properties */
  15. $type = EAN13;
  16. $content = "0075678164125";
  17. /*
  18. * Make the command.
  19. * This is documented on page A-14 of:
  20. * https://files.support.epson.com/pdf/lx300p/lx300pu1.pdf
  21. */
  22. $m = chr(EAN13);
  23. $n = intLowHigh(strlen($content), 2);
  24. $barcodeCommand = Printer::ESC . "G(" . $m . $n . $content;
  25. /* Send it off as usual */
  26. $connector = new FilePrintConnector("php://output");
  27. $printer = new Printer($connector);
  28. $printer->getPrintConnector()->write($barcodeCommand);
  29. $printer->cut();
  30. $printer->close();
  31. /**
  32. * Generate two characters for a number: In lower and higher parts, or more parts as needed.
  33. *
  34. * @param int $input
  35. * Input number
  36. * @param int $length
  37. * The number of bytes to output (1 - 4).
  38. */
  39. function intLowHigh($input, $length)
  40. {
  41. $outp = "";
  42. for ($i = 0; $i < $length; $i ++) {
  43. $outp .= chr($input % 256);
  44. $input = (int) ($input / 256);
  45. }
  46. return $outp;
  47. }
  48. ?>