character-encodings-with-images.php 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /* Change to the correct path if you copy this example! */
  3. require __DIR__ . '/../vendor/autoload.php';
  4. use Mike42\Escpos\Printer;
  5. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  6. use Mike42\Escpos\PrintBuffers\EscposPrintBuffer;
  7. use Mike42\Escpos\PrintBuffers\ImagePrintBuffer;
  8. use Mike42\Escpos\CapabilityProfile;
  9. /**
  10. * This example builds on character-encodings.php, also providing an image-based rendering.
  11. * This is quite slow, since a) the buffers are changed dozens of
  12. * times in the example, and b) It involves sending very wide images, which printers don't like!
  13. *
  14. * There are currently no test cases around the image printing, since it is an experimental feature.
  15. *
  16. * It does, however, illustrate the way that more encodings are available when image output is used.
  17. */
  18. include(dirname(__FILE__) . '/resources/character-encoding-test-strings.inc');
  19. try {
  20. // Enter connector and capability profile
  21. $connector = new FilePrintConnector("php://stdout");
  22. $profile = CapabilityProfile::load('default');
  23. $buffers = array(new EscposPrintBuffer(), new ImagePrintBuffer());
  24. /* Print a series of receipts containing i18n example strings */
  25. $printer = new Printer($connector, $profile);
  26. $printer -> selectPrintMode(Printer::MODE_DOUBLE_HEIGHT | Printer::MODE_EMPHASIZED | Printer::MODE_DOUBLE_WIDTH);
  27. $printer -> text("Implemented languages\n");
  28. $printer -> selectPrintMode();
  29. foreach ($inputsOk as $label => $str) {
  30. $printer -> setEmphasis(true);
  31. $printer -> text($label . ":\n");
  32. $printer -> setEmphasis(false);
  33. foreach ($buffers as $buffer) {
  34. $printer -> setPrintBuffer($buffer);
  35. $printer -> text($str);
  36. }
  37. $printer -> setPrintBuffer($buffers[0]);
  38. }
  39. $printer -> feed();
  40. $printer -> selectPrintMode(Printer::MODE_DOUBLE_HEIGHT | Printer::MODE_EMPHASIZED | Printer::MODE_DOUBLE_WIDTH);
  41. $printer -> text("Works in progress\n");
  42. $printer -> selectPrintMode();
  43. foreach ($inputsNotOk as $label => $str) {
  44. $printer -> setEmphasis(true);
  45. $printer -> text($label . ":\n");
  46. $printer -> setEmphasis(false);
  47. foreach ($buffers as $buffer) {
  48. $printer -> setPrintBuffer($buffer);
  49. $printer -> text($str);
  50. }
  51. $printer -> setPrintBuffer($buffers[0]);
  52. }
  53. $printer -> cut();
  54. /* Close printer */
  55. $printer -> close();
  56. } catch (Exception $e) {
  57. echo "Couldn't print to this printer: " . $e -> getMessage() . "\n";
  58. }