2
votes

I am trying to output an XPS file using the Microsoft XPS Writer from Php with the ESCPOS-php thermal printer writer library found here, written by Mike42, to test print receipts without wasting receipt paper.

I have set the current Printer to "Microsoft XPS Document Writer", and have included the library mentioned in my php website.

I tried printing this webpage (named 'p1PrinterSolution')

function letsPrint()
            {
                require_once(dirname(__FILE__) . "/escpos-php-master/Escpos.php");
                $connector = new FilePrintConnector("Microsoft XPS Document Writer");
                $printer = new Escpos($connector);              
                $printer -> text("Hello World!\n");
                $printer -> cut();
                $printer -> close();

            }
            #let's call the function now kid!
            letsPrint();

However, I am receving this error:

Fatal error: Call to undefined function gzdecode() in (the location of escpos-php) on line 173

If I try to call $printer = new Escpos(); without declaring a connector, I am greeted with this error:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Argument passed to Escpos::__construct() must implement interface PrintConnector, null given.' in (path)\escpos-php-master\Escpos.php:176 Stack trace: #0 (path)\p1PrinterSolution.php(62): Escpos->__construct() #1 {main} thrown in (path)\escpos-php-master\Escpos.php on line 176

How can I setup the ESCPOS-php to print to xps document writer correctly? I am using a windows OS. Windows 7 in particular.

1

1 Answers

1
votes

The immediate error is caused by gzdecode() not existing. It is available on PHP > 5.4. If you upgrade or install the 'zlib' plugin, your code snippet will create a file called 'Microsoft XPS Document Writer' in the current directory, and save some commands to it.

Unless you use 'LPT1' as your printer, escpos-php actually prints on Windows over the network, so you need to share your printer and use its URL to print. There are some examples of this here:

$connector = new WindowsPrintConnector("smb://localhost/Microsoft XPS Document Writer");

However, I would be surprised if the XPS Document writer understands the binary commands (ESC/POS) that escpos-php generates, and there are no free tools (that I know) for rendering ESC/POS commands on a computer to check your work. So this means you will need to waste some receipt paper to make test receipts.

As an alternative way to render receipts, you can create PDF files via some other means, which escpos-php can convert to an image for printing (via the Imagick PHP extension). This slows down printing considerably, but is useful in situations where you need to also email receipts to customers, or want the ability to fall back on a laser printer.

The print-from-pdf.php example shows the API for this, and I've adapted it below to print to LPT1.

<?php
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\ImagickEscposImage;
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;

$pdf = 'resources/document.pdf';
$connector = new WindowsPrintConnector("LPT1");
$printer = new Printer($connector);
try {
    $pages = ImagickEscposImage::loadPdf($pdf);
    foreach ($pages as $page) {
        $printer -> graphics($page);
    }
    $printer -> cut();
} catch (Exception $e) {
  /*
     * loadPdf() throws exceptions if files or not found, or you don't have the
     * imagick extension to read PDF's
     */
    echo $e -> getMessage() . "\n";
} finally {
    $printer -> close();
}