使用Perl CGI打印Zebra标签

时间:2016-06-17 17:14:20

标签: perl cgi zebra-printers

我在从Perl CGI打印Zebra标签时遇到问题,它在一台服务器上运行但在另一台服务器上运行。此外,如果我从命令行运行该程序,它可以在任一服务器上运行。服务器是IIS 7(不要笑它是我用的东西)。

以下是代码:

run [ -p path ] command [ -wait ] arguments

我猜测它与在CGI中打开套接字有关,但我没有多少经验。

提前致谢

1 个答案:

答案 0 :(得分:1)

事实证明,我们的Zebra打印机在打印标签后正在发送响应并等待验证它已经交付,这将其锁定。到目前为止工作的解决方案是获取响应,但为了以防万一,还在套接字上设置了一个短暂的超时。还上了食物链并使用了IO :: Socket而不是旧的Socket库:

use strict;
use IO::Socket;
use CGI qw(:cgi-lib);
use CGI::Carp qw ( fatalsToBrowser );
my %formdata = Vars;

print "Content-type: text/html\r\n\r\n";

# to test running from the command line, hardcode the paramters normally passed from the web interface
# comment these out when running CGI
$formdata{printer} = "zebralabel1.mycompany.com";
$formdata{serials} = "TR16170003|Gerry's Product TR|This is a generic product where all serial numbers start with the letters TR|T~";

# create the socket, connect to the port
my $remote = IO::Socket::INET->new(
                Proto   => 'tcp',
                PeerAddr=> "$formdata{printer}",
                PeerPort=> "9100",
                ReuseAddr=> 0,
                Timeout  => 2,
                ) or myExit("Cannot connect to printer: $!");
$remote->autoflush(1);  # Send immediately

my ($serial, $product, $desc) = split(/\|/, $formdata{serials});
# example formatted label
my $line = qq~^XA^PRA,A,A^LH20,20^FO20,40^FWN^AT,60,10^FD Serial Number: $serial^FS^FO20,120^FWN^AT,60,10^FD Product: $product^FS^FO20,200^FWN^AT,60,10^FD Description: $desc^FS^FO50,280^B3N,N,100,Y,N^FD$serial^FS^XZ~;

print $remote $line;
my $dontCare = <remote>;

close $remote;
myExit("Labels Printed.");

sub myExit
{
    my $msg = shift;
    print "<script>alert('$msg')</script>";
    exit;
}