如何在 PHP 脚本中编写 ZPL 代码以及如何将其发送到 Zebra 打印机进行打印


how to write a zpl code inside a php script and how to send it to a zebra printer for printing

我想使用 zebra 标签打印机在标签上打印条形码。条码打印机型号为斑马GK420d。贴纸打印区域为 5 厘米 x 10 厘米.我想从 php 脚本中执行此操作。通过谷歌搜索,我找到了一些示例并以这种方式实现

$barcode = "sometext";
$labelcode =<<<AAA
^XA
^FO100,75
^BCN, 100,Y, N,
^FD$barcode^FS
^XZ
AAA;
file_put_contents('/dev/lpt1',$labelcode);

当我连接打印机并进行测试时,它会起作用吗?我必须为此斑马打印机应用哪些设置才能打印。我不知道斑马打印机的设置。此外,file_put_contents将使用端口将代码复制到打印机。如何找到连接到系统的打印机的端口。如果通过 USB 将哪些信息传递给file_put_contents。请建议斑马打印工艺

虽然您可以假设将原始 ZPL/EPL 命令发送到打印机设备,但如果您还不知道 ZPL/EPL,并且您已经可以在您的环境中生成图像,则最好不要这样做。

你的代码意味着你在一个类Unix的系统上。 如果您使用的是最近的类Unix系统,则打印应由CUPS控制。 Zebra 发布了不受支持但大部分功能正常的 CUPS 支持文件。

在 CUPS 中设置打印机,/usr/bin/lp然后将 -d 标志设置为打印机名称,-o ppi=...值设置为打印机的 DPI,以及可能用于强制对齐或纵向/横向模式的其他内容。 GK420s是203 DPI打印机,因此您至少需要-o ppi=203

然后,您可以将任何内容打印到 CUPS 可以理解的打印机,包括图像和 PDF 文档。 这允许您在 PHP 端合成您想要的任何内容,而不限制您使用打印机理解的命令语言。 例如,我们使用wkhtmltoimage来构建运输标签,而我们使用GD和PEAR的史前Image_Barcode来制作小的条形码标签。 顺便说一下,有更好的选择。

或者,您可以在 CUPS 中设置"通用原始"虚拟打印机。 然后,您可以直接通过该打印机打印命令语言文本文件。 您可能只有在熟悉 EPL 或 ZPL 的情况下才应该这样做。

以下代码是我们用于打印到所有打印机(包括 Zebras(的真实实时代码的精简部分。 只需调用下面的函数,将要打印的数据(例如图像的内容、文本或其他内容(作为第一个参数。

function print_example($data) {
// You'll need to change these according to your local names and options.
    $server = 'printserver.companyname.com';
    $printer_name = 'Zebra_ZP_500_KB'; // That's effectively the same thing as your GK420d
    $options_flag = '-o position=bottom-left,ppi=203,landscape';
    $process_name = 'LC_ALL=en_US.UTF-8 /usr/bin/lp -h %s -d %s %s';
    $command = sprintf($process_name, $server, $printer_name, (string)$options_flag);
    $pipes = array();
    $process = proc_open($command, $handles, $pipes);
// Couldn't open the pipe -- shouldn't happen
    if (!is_resource($process))
        trigger_error('Printing failed, proc_open() did not return a valid resource handle', E_USER_FATAL);
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// As we've been given data to write directly, let's kinda like do that.
    fwrite($pipes[0], $data);
    fclose($pipes[0]);
// 1 => readable handle connected to child stdout
    $stdout = fgets($pipes[1]);
    fclose($pipes[1]);
// 2 => readable handle connected to child stderr
    $stderr = fgets($pipes[2]);
    fclose($pipes[2]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
    $return_value = proc_close($process);
// We've asked lp not to be quiet about submitting jobs so we can make
// sure that the print job was submitted.
    $request_match = array();
    if (!preg_match('/request id is'b(.+)/', $stdout, $request_match)) {
        add_warning("Print to '$printer' failed.  Please check the printer status.");
        return false;
    }
    add_notice("Print to '$printer' succeeded.  Job $request_match[1].");
    return true;
}

函数add_warningadd_notice在我们的代码中实现,您需要根据实际打印的内容替换它们。