【发布时间】:2018-12-30 00:16:48
【问题描述】:
我们正在为货币兑换商开发基于 PHP 的 Web 应用程序。我们正在使用的货币兑换商使用 POS (EPSON TM-U220D) 打印机作为客户收据,因此我们正在寻找一种从 Web 应用程序打印到 POS 打印机的方法,我们尝试了 mike42/escpos-php PHP 库并使用它让 USB 收据打印机在 Windows 教程上工作,但它不起作用。这是我们第一次使用 POS 打印机,所以我们完全迷路了。有人可以帮忙吗?提前致谢。 详情:
Printer : [EPSON TM-U220D][1]
PHP Library : [Mike42/Escpos-php][1]
Tutorial : [Getting a USB receipt printer working on Windows][1]
Programming Languages : [PHP 7.2.0][1]
Web Server : [Apache][1]
Database : [MySQL][1]
代码:
/*
// Call this file 'hello-world.php'
require __DIR__ . '/inc/escpos/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
$connector = new FilePrintConnector("php://stdout");
$printer = new Printer($connector);
$printer -> text("Hello World!\n");
$printer -> cut();
$printer -> close();
*/
/* Change to the correct path if you copy this example! */
require __DIR__ . '/inc/escpos/vendor/autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
/**
* Install the printer using USB printing support, and the "Generic / Text Only" driver,
* then share it (you can use a firewall so that it can only be seen locally).
*
* Use a WindowsPrintConnector with the share name to print.
*
* Troubleshooting: Fire up a command prompt, and ensure that (if your printer is shared as
* "Receipt Printer), the following commands work:
*
* echo "Hello World" > testfile
* copy testfile "\\%COMPUTERNAME%\Receipt Printer"
* del testfile
*/
try {
// Enter the share name for your USB printer here
$connector = null;
//$connector = new WindowsPrintConnector("Receipt Printer");
/* Print a "Hello world" receipt" */
$printer = new Printer($connector);
$printer -> text("Hello World!\n");
$printer -> cut();
/* Close printer */
$printer -> close();
} catch (Exception $e) {
echo "Couldn't print to this printer: " . $e -> getMessage() . "\n";
}
【问题讨论】:
-
到目前为止,您尝试了哪些方法来调试问题? “不起作用”究竟是什么意思?你有错误吗?如果有,是哪一个?请看how to ask
-
致命错误:未捕获的 TypeError:参数 1 传递给 Mike42\Escpos\Printer::__construct() 必须是 Mike42\Escpos\PrintConnectors\PrintConnector 的实例,给定 null,在 C:\xampp 中调用\htdocs\runa\pos.php 位于第 37 行,定义在 C:\xampp\htdocs\runa\inc\escpos\vendor\mike42\escpos-php\src\Mike42\Escpos\Printer.php:361 堆栈跟踪:# 0 C:\xampp\htdocs\runa\pos.php(37): Mike42\Escpos\Printer->__construct(NULL) #1 {main} 在 C:\xampp\htdocs\runa\inc\escpos\vendor\ mike42\escpos-php\src\Mike42\Escpos\Printer.php 在第 361 行
-
错误清楚地说明了问题所在,当需要 PrintConnector 实例时传递 null。这听起来很像您希望其他人为您完成工作。请参考我再次链接的如何询问页面。
标签: php printing pos posprinter php-printer