【问题标题】:how to write a zpl code inside a php script and how to send it to a zebra printer for printing如何在 php 脚本中编写 zpl 代码以及如何将其发送到斑马打印机进行打印
【发布时间】:2013-10-11 11:26:01
【问题描述】:

我想使用斑马标签打印机在标签上打印条码。条码打印机型号为 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。请建议斑马打印过程

【问题讨论】:

标签: php printing zebra-printers barcode-printing


【解决方案1】:

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

您的代码暗示您使用的是类 Unix 系统。如果您使用的是最近类 Unix 系统,那么打印应该由CUPS 控制。 Zebra 已发布unsupported but mostly functional CUPS support files

在 CUPS 中设置打印机,然后使用 -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 函数在我们的代码中实现,您需要根据实际打印的内容替换它们。

【讨论】:

  • 我想我需要您的解决方案“然后您可以在 CUPS 可以理解的打印机上打印任何内容,包括图像和 PDF 文档。这允许您在 PHP 端合成任何您想要的东西”,就像我的情况一样我作为一个菜鸟认为我可以吐出我用 PHP 生成的徽章 pdf 布局(请参阅我的徽章布局问题:stackoverflow.com/questions/34771587/…),但这也适用于 Windows,我可以打印到 Google Chrome ZPL 插件进行测试吗?
  • 对不起,我的意思是这个问题:stackoverflow.com/questions/34782567/…
  • 不幸的是,我对让 PHP 从 Windows 打印一无所知。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-15
  • 1970-01-01
  • 2017-02-03
  • 1970-01-01
相关资源
最近更新 更多