print-from-html.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. $connector = new FilePrintConnector("php://stdout"); // Add connector for your printer here.
  7. $printer = new Printer($connector);
  8. /*
  9. * Due to its complxity, escpos-php does not support HTML input. To print HTML,
  10. * either convert it to calls on the Printer() object, or rasterise the page with
  11. * wkhtmltopdf, an external package which is designed to handle HTML efficiently.
  12. *
  13. * This example is provided to get you started: On Debian, first run-
  14. *
  15. * sudo apt-get install wkhtmltopdf xvfb
  16. *
  17. * Note: Depending on the height of your pages, it is suggested that you chop it
  18. * into smaller sections, as printers simply don't have the buffer capacity for
  19. * very large images.
  20. *
  21. * As always, you can trade off quality for capacity by halving the width
  22. * (550 -> 225 below) and printing w/ Escpos::IMG_DOUBLE_WIDTH | Escpos::IMG_DOUBLE_HEIGHT
  23. */
  24. try {
  25. /* Set up command */
  26. $source = __DIR__ . "/resources/document.html";
  27. $width = 550;
  28. $dest = tempnam(sys_get_temp_dir(), 'escpos') . ".png";
  29. $command = sprintf(
  30. "xvfb-run wkhtmltoimage -n -q --width %s %s %s",
  31. escapeshellarg($width),
  32. escapeshellarg($source),
  33. escapeshellarg($dest)
  34. );
  35. /* Test for dependencies */
  36. foreach (array("xvfb-run", "wkhtmltoimage") as $cmd) {
  37. $testCmd = sprintf("which %s", escapeshellarg($cmd));
  38. exec($testCmd, $testOut, $testStatus);
  39. if ($testStatus != 0) {
  40. throw new Exception("You require $cmd but it could not be found");
  41. }
  42. }
  43. /* Run wkhtmltoimage */
  44. $descriptors = array(
  45. 1 => array("pipe", "w"),
  46. 2 => array("pipe", "w"),
  47. );
  48. $process = proc_open($command, $descriptors, $fd);
  49. if (is_resource($process)) {
  50. /* Read stdout */
  51. $outputStr = stream_get_contents($fd[1]);
  52. fclose($fd[1]);
  53. /* Read stderr */
  54. $errorStr = stream_get_contents($fd[2]);
  55. fclose($fd[2]);
  56. /* Finish up */
  57. $retval = proc_close($process);
  58. if ($retval != 0) {
  59. throw new Exception("Command $cmd failed: $outputStr $errorStr");
  60. }
  61. } else {
  62. throw new Exception("Command '$cmd' failed to start.");
  63. }
  64. /* Load up the image */
  65. try {
  66. $img = EscposImage::load($dest);
  67. } catch (Exception $e) {
  68. unlink($dest);
  69. throw $e;
  70. }
  71. unlink($dest);
  72. /* Print it */
  73. $printer -> bitImage($img); // bitImage() seems to allow larger images than graphics() on the TM-T20. bitImageColumnFormat() is another option.
  74. $printer -> cut();
  75. } catch (Exception $e) {
  76. echo $e -> getMessage();
  77. } finally {
  78. $printer -> close();
  79. }