1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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\WindowsPrintConnector;
  6. /**
  7. * Install the printer using USB printing support, and the "Generic / Text Only" driver,
  8. * then share it.
  9. *
  10. * Use a WindowsPrintConnector with the share name to print. This works on either
  11. * Windows or Linux.
  12. *
  13. * Troubleshooting: Fire up a command prompt/terminal, and ensure that (if your printer is
  14. * shared as "Receipt Printer"), the following commands work.
  15. *
  16. * Windows: (use an appropriate "net use" command if you need authentication)
  17. * echo "Hello World" > testfile
  18. * ## If you need authentication, use "net use" to hook up the printer:
  19. * # net use "\\computername\Receipt Printer" /user:Guest
  20. * # net use "\\computername\Receipt Printer" /user:Bob secret
  21. * # net use "\\computername\Receipt Printer" /user:workgroup\Bob secret
  22. * copy testfile "\\computername\Receipt Printer"
  23. * del testfile
  24. *
  25. * GNU/Linux:
  26. * # No authentication
  27. * echo "Hello World" | smbclient "//computername/Receipt Printer" -c "print -" -N
  28. * # Guest login
  29. * echo "Hello World" | smbclient "//computername/Receipt Printer" -U Guest -c "print -" -N
  30. * # Basic username/password
  31. * echo "Hello World" | smbclient "//computername/Receipt Printer" secret -U "Bob" -c "print -"
  32. * # Including domain name
  33. * echo "Hello World" | smbclient "//computername/Receipt Printer" secret -U "workgroup\\Bob" -c "print -"
  34. */
  35. try {
  36. // Enter the share name for your printer here, as a smb:// url format
  37. $connector = new WindowsPrintConnector("smb://computername/Receipt Printer");
  38. //$connector = new WindowsPrintConnector("smb://Guest@computername/Receipt Printer");
  39. //$connector = new WindowsPrintConnector("smb://FooUser:secret@computername/workgroup/Receipt Printer");
  40. //$connector = new WindowsPrintConnector("smb://User:secret@computername/Receipt Printer");
  41. /* Print a "Hello world" receipt" */
  42. $printer = new Printer($connector);
  43. $printer -> text("Hello World!\n");
  44. $printer -> cut();
  45. /* Close printer */
  46. $printer -> close();
  47. } catch (Exception $e) {
  48. echo "Couldn't print to this printer: " . $e -> getMessage() . "\n";
  49. }