虽然您可以假设将原始 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_warning 和 add_notice 函数在我们的代码中实现,您需要根据实际打印的内容替换它们。