如何在 php 脚本中编写 zpl 代码以及如何将其发送到 zebra 打印机进行打印 [英] how to write a zpl code inside a php script and how to send it to a zebra printer for printing

查看:125
本文介绍了如何在 php 脚本中编写 zpl 代码以及如何将其发送到 zebra 打印机进行打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 zebra 标签打印机在标签上打印条码.条码打印机型号为 Zebra GK420d.贴纸打印区域为 5 cm x 10 cm.我想从 php 脚本中完成.通过谷歌搜索,我找到了一些示例并以这种方式实现

I want to print a bar code on a label using zebra label printer.Barcode printer model is Zebra GK420d . Sticker print area is 5 cm by 10 cm.I want to do it from a php script.By googling I found some examples and implemented in this way

$barcode = "sometext";
$labelcode =<<<AAA
^XA
^FO100,75
^BCN, 100,Y, N,
^FD$barcode^FS
^XZ
AAA;

file_put_contents('/dev/lpt1',$labelcode);

当我连接打印机并测试时它会工作吗?我必须为这台 zebra 打印机应用哪些设置才能打印.我不知道 zebra 打印机的设置.而且 file_put_contents 会将代码复制到打印机通过使用端口.如何找到连接到系统的打印机的端口.如果通过usb,我们必须将什么信息传递给file_put_contents.请建议斑马打印过程

Will it work when i connect the printer and test?What are the settings that i have to apply for this zebra printer in order to print.I have no idea on zebra printers settings.And also file_put_contents will copy the code to the printer by using the port.how to find the port of the printer that is connected to the system.if it by usb what info we have to pass to the file_put_contents.Please suggest the zebra printing process

推荐答案

虽然您可以假设将原始 ZPL/EPL 命令发送到打印机设备,但如果您还不知道 ZPL/EPL,如果您已经可以在您的环境中生成图像.

While you could hypothetically send the raw ZPL / EPL commands to the printer device, you might be better off not doing that if you don't already know ZPL / EPL, and if you can already generate images in your environment.

您的代码意味着您使用的是类 Unix 系统.如果您使用的是最近类 Unix 系统,则打印应由 控制杯子.Zebra 已发布不受支持但大部分功能的 CUPS 支持文件.

Your code implies that you're on a Unix-like system. If you're on a recent Unix-like system, then printing should be controlled by CUPS. Zebra has published unsupported but mostly functional CUPS support files.

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

Set up the printer in CUPS, then shell out to /usr/bin/lp with the -d flag set to the printer name, and a -o ppi=... value to set the DPI of the printer, and possibly other things to force alignment or portrait/landscape mode. That GK420s is a 203 DPI printer, so you'd want -o ppi=203 at the very minimum.

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

You can then print anything to that printer that CUPS can understand, including images and PDF documents. This allows you to composite anything you want PHP-side, not restricting you to command language that the printer understands. For example, we use wkhtmltoimage to build shipping labels, while we use GD and PEAR's prehistoric Image_Barcode to produce small barcode labels. There are better options for that, by the way.

或者,您可以在 CUPS 中设置Generic Raw"虚拟打印机.然后您可以直接通过该打印机打印命令语言文本文件.如果您对 EPL 或 ZPL 感到舒适和熟悉,那么您可能只应该这样做.

Alternatively, you can set up a "Generic Raw" virtual printer in CUPS. You can then print command language text files directly through that printer. You should probably only do it that way if you are comfortable and familiar with EPL or ZPL.

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

The following code is a cut down section of real, live code that we use to print to all of our printers, including Zebras. Just call the function below with the data you want printed (such as the contents of an image, or text, or whatever) as the first argument.

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 函数在我们的代码中实现,您需要根据实际打印的内容适当替换它们.

The functions add_warning and add_notice are implemented in our code, you'll need to replace them as appropriate for whatever you're actually printing.

这篇关于如何在 php 脚本中编写 zpl 代码以及如何将其发送到 zebra 打印机进行打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆