linux-usb.php 1.1KB

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. /* Change to the correct path if you copy this example! */
  3. require __DIR__ . '/../../autoload.php';
  4. use Mike42\Escpos\Printer;
  5. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  6. /**
  7. * On Linux, use the usblp module to make your printer available as a device
  8. * file. This is generally the default behaviour if you don't install any
  9. * vendor drivers.
  10. *
  11. * Once this is done, use a FilePrintConnector to open the device.
  12. *
  13. * Troubleshooting: On Debian, you must be in the lp group to access this file.
  14. * dmesg to see what happens when you plug in your printer to make sure no
  15. * other drivers are unloading the module.
  16. */
  17. try {
  18. // Enter the device file for your USB printer here
  19. $connector = new FilePrintConnector("/dev/usb/lp0");
  20. //$connector = new FilePrintConnector("/dev/usb/lp1");
  21. //$connector = new FilePrintConnector("/dev/usb/lp2");
  22. /* Print a "Hello world" receipt" */
  23. $printer = new Printer($connector);
  24. $printer -> text("Hello World!\n");
  25. $printer -> cut();
  26. /* Close printer */
  27. $printer -> close();
  28. } catch (Exception $e) {
  29. echo "Couldn't print to this printer: " . $e -> getMessage() . "\n";
  30. }