print-from-pdf.php 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. require __DIR__ . '/../vendor/autoload.php';
  3. use Mike42\Escpos\Printer;
  4. use Mike42\Escpos\ImagickEscposImage;
  5. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  6. /*
  7. * This is three examples in one:
  8. * 1: Print an entire PDF, normal quality.
  9. * 2: Print at a lower quality for speed increase (CPU & transfer)
  10. * 3: Cache rendered documents for a speed increase (removes CPU image processing completely on subsequent prints)
  11. */
  12. /* 1: Print an entire PDF, start-to-finish (shorter form of the example) */
  13. $pdf = 'resources/document.pdf';
  14. $connector = new FilePrintConnector("php://stdout");
  15. $printer = new Printer($connector);
  16. try {
  17. $pages = ImagickEscposImage::loadPdf($pdf);
  18. foreach ($pages as $page) {
  19. $printer -> graphics($page);
  20. }
  21. $printer -> cut();
  22. } catch (Exception $e) {
  23. /*
  24. * loadPdf() throws exceptions if files or not found, or you don't have the
  25. * imagick extension to read PDF's
  26. */
  27. echo $e -> getMessage() . "\n";
  28. } finally {
  29. $printer -> close();
  30. }
  31. /*
  32. * 2: Speed up printing by roughly halving the resolution, and printing double-size.
  33. * This gives a 75% speed increase at the expense of some quality.
  34. *
  35. * Reduce the page width further if necessary: if it extends past the printing area, your prints will be very slow.
  36. */
  37. $connector = new FilePrintConnector("php://stdout");
  38. $printer = new Printer($connector);
  39. $pdf = 'resources/document.pdf';
  40. $pages = ImagickEscposImage::loadPdf($pdf, 260);
  41. foreach ($pages as $page) {
  42. $printer -> graphics($page, Printer::IMG_DOUBLE_HEIGHT | Printer::IMG_DOUBLE_WIDTH);
  43. }
  44. $printer -> cut();
  45. $printer -> close();
  46. /*
  47. * 3: PDF printing still too slow? If you regularly print the same files, serialize & compress your
  48. * EscposImage objects (after printing[1]), instead of throwing them away.
  49. *
  50. * (You can also do this to print logos on computers which don't have an
  51. * image processing library, by preparing a serialized version of your logo on your PC)
  52. *
  53. * [1]After printing, the pixels are loaded and formatted for the print command you used, so even a raspberry pi can print complex PDF's quickly.
  54. */
  55. $connector = new FilePrintConnector("php://stdout");
  56. $printer = new Printer($connector);
  57. $pdf = 'resources/document.pdf';
  58. $ser = 'resources/document.z';
  59. if (!file_exists($ser)) {
  60. $pages = ImagickEscposImage::loadPdf($pdf);
  61. } else {
  62. $pages = unserialize(gzuncompress(file_get_contents($ser)));
  63. }
  64. foreach ($pages as $page) {
  65. $printer -> graphics($page);
  66. }
  67. $printer -> cut();
  68. $printer -> close();
  69. if (!file_exists($ser)) {
  70. file_put_contents($ser, gzcompress(serialize($pages)));
  71. }