我正在尝试使用Php的Microsoft XPS编写器输出XPS文件,其中包含此处由Mike42编写的ESCPOS-php热敏打印机编写器库,以测试打印收据而不会浪费收据纸。
我已将当前打印机设置为"Microsoft XPS 文档编写器",并包含我的 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-masterEscpos.php:176 Stack trace: #0 (path)p1PrinterSolution.php(62): Escpos->__construct() #1 {main} thrown in (path)escpos-php-masterEscpos.php on line 176
如何设置 ESCPOS-php 以正确打印到 xps 文档编写器?我使用的是Windows操作系统,特别是Windows 7。
直接错误是由 gzdecode() 不存在引起的。它在 PHP> 5.4 上可用。如果升级或安装"zlib"插件,则代码片段将在当前目录中创建一个名为"Microsoft XPS Document Writer"的文件,并向其保存一些命令。
除非您使用"LPT1"作为打印机,否则escpos-php实际上通过网络在Windows上打印,因此您需要共享打印机并使用其URL进行打印。这里有一些这样的例子:
$connector = new WindowsPrintConnector("smb://localhost/Microsoft XPS Document Writer");
但是,如果XPS文档编写者理解escpos-php生成的二进制命令(ESC/POS),并且没有免费工具(我知道)可以在计算机上呈现ESC/POS命令来检查您的工作,我会感到惊讶。因此,这意味着您需要浪费一些收据纸来制作测试收据。
作为呈现收据的另一种方式,您可以通过其他方式创建PDF文件,escpos-php可以将其转换为图像进行打印(通过Imagick PHP扩展)。这会大大减慢打印速度,但在您还需要通过电子邮件将收据发送给客户或希望能够回退到激光打印机的情况下很有用。
从 pdf 打印.php示例显示了这方面的 API,我在下面对其进行了调整以打印到 LPT1
.
<?php
require __DIR__ . '/vendor/autoload.php';
use Mike42EscposPrinter;
use Mike42EscposImagickEscposImage;
use Mike42EscposPrintConnectorsWindowsPrintConnector;
$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();
}