Escpos-php 带串口打印机

Escpos-php with serial printer

我正在尝试使用 php 使用我的 Epson TM-T88IV 串行打印机打印二维码。但是,我的 php 文件安装在服务器上,我能够从 html 文件成功调用它。我正在使用名为 ESCPOS-PHP (https://github.com/mike42/escpos-php) 的库,计算机是 运行 Windows XP Professional。这是我的 php 片段(中间还有更多,但打印操作不需要):

<?php
require __DIR__. '/escpos-php-master/Escpos.php'; 
use Mike42\Escpos\Printer; 
use Mike42\Escpos\PrintConnectors\FilePrintConnector;

[...]

try {
    $connector = new WindowsPrintConnector("EPSON TM-T88IV Receipt");
    $printer = new Escpos($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";
}
?>

好像我无法连接到打印机。我也试过

$connector = new FilePrintConnector("/dev/ttyS0");
$printer = new Printer($connector);

这应该是串行打印机的方式(我不确定我应该放什么而不是“/dev/ttsyS0”)。也许我不应该尝试通过服务器触发它?我这样做是因为我无法修改他的 POS 系统 (Maitre D),而且我需要一种简单的方法来在账单上打印二维码。如果您知道任何解决方法,我们将不胜感激!谢谢

escpos-php 的作者。

escpos-php README 建议您首先尝试在命令行上向您的打印机发送数据,因为它可以让您在尝试使用驱动程序之前确定您将如何打印。

例如,如果您打算在 COM1 上设置打印机,您可以尝试键入:

echo "Hello world" > COM1

对应于:

<?php
$connector = new FilePrintConnector("COM1");
$printer = new Escpos($connector);
$printer -> text("Hello World\n");

WindowsPrintConnector 用于连接 Windows 共享打印机。 This example 有一些有用的命令来确保您可以在打开 PHP 之前进行打印。例如,

echo "Hello world" > foo.txt
net use "\computername\Receipt Printer" /user:Bob secret
copy testfile "\computername\Receipt Printer"
del testfile

这对应于:

<?php
$connector = new WindowsPrintConnector("smb://bob:secret@computername/Receipt Printer");
$printer = new Escpos($connector);
$printer -> text("Hello World\n");

无论如何,有两个陷阱:

  • 仅与 Generic/Text 驱动程序一起使用。 escpos-php 将输出原始 ESC/POS,这将混淆任何需要文档的驱动程序。
  • 这是服务器端。确保 PHP 在将执行打印的计算机上是 运行,或者如果使用网络打印,您至少在内部网络上。