Inheritance: extends EscposImage
Example #1
1
foreach ($pages as $page) {
    $printer->graphics($page, Printer::IMG_DOUBLE_HEIGHT | Printer::IMG_DOUBLE_WIDTH);
}
$printer->cut();
$printer->close();
/*
 * 3: PDF printing still too slow? If you regularly print the same files, serialize & compress your
 * EscposImage objects (after printing[1]), instead of throwing them away.
 * 
 * (You can also do this to print logos on computers which don't have an
 * image processing library, by preparing a serialized version of your logo on your PC)
 * 
 * [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.
 */
$connector = new FilePrintConnector("php://stdout");
$printer = new Printer($connector);
$pdf = 'resources/document.pdf';
$ser = 'resources/document.z';
if (!file_exists($ser)) {
    $pages = ImagickEscposImage::loadPdf($pdf);
} else {
    $pages = unserialize(gzuncompress(file_get_contents($ser)));
}
foreach ($pages as $page) {
    $printer->graphics($page);
}
$printer->cut();
$printer->close();
if (!file_exists($ser)) {
    file_put_contents($ser, gzcompress(serialize($pages)));
}
Example #2
0
 public function writeText($text)
 {
     if ($this->printer == null) {
         throw new LogicException("Not attached to a printer.");
     }
     if ($text == null) {
         return;
     }
     $text = trim($text, "\n");
     /* Create Imagick objects */
     $image = new \Imagick();
     $draw = new \ImagickDraw();
     $color = new \ImagickPixel('#000000');
     $background = new \ImagickPixel('white');
     /* Create annotation */
     if ($this->font !== null) {
         // Allow fallback on defaults as necessary
         $draw->setFont($this->font);
     }
     /* In Arial, size 21 looks good as a substitute for FONT_B, 24 for FONT_A */
     $draw->setFontSize($this->fontSize);
     $draw->setFillColor($color);
     $draw->setStrokeAntialias(true);
     $draw->setTextAntialias(true);
     $metrics = $image->queryFontMetrics($draw, $text);
     $draw->annotation(0, $metrics['ascender'], $text);
     /* Create image & draw annotation on it */
     $image->newImage($metrics['textWidth'], $metrics['textHeight'], $background);
     $image->setImageFormat('png');
     $image->drawImage($draw);
     // debugging if you want to view the images yourself
     //$image -> writeImage("test.png");
     /* Save image */
     $escposImage = new ImagickEscposImage();
     $escposImage->readImageFromImagick($image);
     $size = Printer::IMG_DEFAULT;
     $this->printer->bitImage($escposImage, $size);
 }
Example #3
0
 /**
  * Load a PDF for use on the printer
  *
  * @param string $pdfFile
  *  The file to load
  * @param string $pageWidth
  *  The width, in pixels, of the printer's output. The first page of the
  *  PDF will be scaled to approximately fit in this area.
  * @throws Exception Where Imagick is not loaded, or where a missing file
  *  or invalid page number is requested.
  * @return multitype:EscposImage Array of images, retrieved from the PDF file.
  */
 public static function loadPdf($pdfFile, $pageWidth = 550)
 {
     if (!EscposImage::isImagickLoaded()) {
         throw new Exception(__FUNCTION__ . " requires imagick extension.");
     }
     /*
      * Load first page at very low density (resolution), to figure out what
      * density to use to achieve $pageWidth
      */
     try {
         $image = new \Imagick();
         $testRes = 2;
         // Test resolution
         $image->setresolution($testRes, $testRes);
         /* Load document just to measure geometry */
         $image->readimage($pdfFile);
         $geo = $image->getimagegeometry();
         $image->destroy();
         $width = $geo['width'];
         $newRes = $pageWidth / $width * $testRes;
         /* Load entire document in */
         $image->setresolution($newRes, $newRes);
         $image->readImage($pdfFile);
         $pages = $image->getNumberImages();
         /* Convert images to Escpos objects */
         $ret = array();
         for ($i = 0; $i < $pages; $i++) {
             $image->setIteratorIndex($i);
             $ep = new ImagickEscposImage();
             $ep->readImageFromImagick($image);
             $ret[] = $ep;
         }
         return $ret;
     } catch (\ImagickException $e) {
         /* Wrap in normal exception, so that classes which call this do not
          * themselves require imagick as a dependency. */
         throw new Exception($e);
     }
 }
Example #4
0
<?php

/*
 * Example of one way you could load a PNG data URI into an EscposImage object
 * without using a file.
 */
require __DIR__ . '/../../autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\ImagickEscposImage;
// Data URI for a PNG image (red dot from https://en.wikipedia.org/wiki/Data_URI_scheme )
$uri = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA\nAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO\n9TXL0Y4OHwAAAABJRU5ErkJggg==";
// Convert data URI to binary data
$imageBlob = base64_decode(explode(",", $uri)[1]);
// Give Imagick a filename with the correct extension to stop it from attempting
// to identify the format itself (this avoids CVE-2016–3714)
$imagick = new Imagick();
$imagick->setResourceLimit(6, 1);
// Prevent libgomp1 segfaults, grumble grumble.
$imagick->readImageBlob($imageBlob, "input.png");
// Load Imagick straight into an EscposImage object
$im = new ImagickEscposImage();
$im->readImageFromImagick($imagick);
// Do a test print to make sure that this EscposImage object has the right data
// (should see a tiny bullet point)
$connector = new FilePrintConnector("php://output");
$printer = new Printer($connector);
$printer->bitImage($im);
$printer->cut();
$printer->close();