sri*_*h s 0 php printing zebra-printers barcode-printing
我想使用 zebra 标签打印机在标签上打印条码。条码打印机型号是 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);
Run Code Online (Sandbox Code Playgroud)
当我连接打印机并测试时它会工作吗?我必须为这台 zebra 打印机应用哪些设置才能打印。我不知道 zebra 打印机的设置。而且 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 中设置“Generic Raw”虚拟打印机。然后您可以直接通过该打印机打印命令语言文本文件。如果您对 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;
}
Run Code Online (Sandbox Code Playgroud)
函数add_warning和add_notice在我们的代码中实现,您需要根据实际打印的内容适当替换它们。