ESCPOS-php打印到XPS文档编写器

时间:2016-02-18 06:21:11

标签: php escpos

我正在尝试使用来自Php的Microsoft XPS Writer输出一个XPS文件,其中找到了由Mike42编写的ESCPOS-php热敏打印机编写器库here来测试打印收据,而不会浪费收据纸。

我已将当前的打印机设置为“Microsoft XPS Document Writer”,并已包含我的php网站中提到的库。

我尝试打印此网页(名为“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();

但是,我收到了这个错误:

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

如果我在没有声明连接器的情况下尝试拨打$printer = new Escpos();,我会收到此错误:

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

如何设置ESCPOS-php以正确打印到xps文档编写器? 我正在使用Windows操作系统。特别是Windows 7。

1 个答案:

答案 0 :(得分:1)

即时错误是由gzdecode()不存在引起的。它可以在PHP上获得> 5.4。如果你升级或安装'zlib'插件,你的代码片段将在当前目录中创建一个名为'Microsoft XPS Document Writer'的文件,并保存一些命令。

除非您使用'LPT1'作为您的打印机,否则escpos-php实际上是通过网络在Windows上打印的,因此您需要共享打印机并使用其URL进行打印。有一些例子here

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

但是,如果XPS Document编写器理解escpos-php生成的二进制命令(ESC / POS),并且没有用于在计算机上渲染ESC / POS命令的免费工具(我知道),我会感到惊讶检查你的工作。所以这意味着你需要浪费一些收据纸来制作测试收据。

作为渲染收据的另一种方法,您可以通过其他方式创建PDF文件,escpos-php可以转换为图像进行打印(通过Imagick PHP扩展)。这会大大减慢打印速度,但在需要向客户发送收据或希望能够使用激光打印机的情况下也很有用。

print-from-pdf.php示例显示了此API,我在下面对其进行了调整以打印到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();
}