Operating System - Linux
1822727 Members
3842 Online
109644 Solutions
New Discussion юеВ

Re: Perl Network Printing Script

 
SOLVED
Go to solution
Junior C.
Frequent Advisor

Perl Network Printing Script

I need some help in writing a perl script to print to a network printer from windowsXP.

Searching Google I found info indicating that I have to open a socket to the destination IP address and port and dump the data which will
transparently be sent to the printer. What module do I used to open the socket?

use strict;
my $port='xxxx';
my $default_printer='xx.xx.xx.xx';

open(OUTPUT_PRINTER,"> \\\\$default_printer\\$port") || die "Unable to
open printer: $!\n"; print OUTPUT_PRINTER "This is a test Line\n";


Appreciate all help.


JC


12 REPLIES 12
Court Campbell
Honored Contributor

Re: Perl Network Printing Script

try Net::LPR
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Court Campbell
Honored Contributor

Re: Perl Network Printing Script

I guess a link may help:

http://search.cpan.org/~dmlloyd/Net-LPR-1.007/LPR.pod
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Court Campbell
Honored Contributor

Re: Perl Network Printing Script

Net::printer also looks promising.

http://search.cpan.org/~cfuhrman/Net-Printer-1.04/lib/Net/Printer.pm
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Rob Leadbeater
Honored Contributor

Re: Perl Network Printing Script

Hi,

Network printers use a variety of port numbers.

Common ones are 9100 for JetDirects, and 515 for LPD based printers.

What printer are you using specifically ?

Hope this helps,

Regards,

Rob
Alexander Chuzhoy
Honored Contributor

Re: Perl Network Printing Script

The following will work:
use IO::Socket;
my $remote_host = 'x.x.x.x';
my $remote_port = 'x';
my $socket = IO::Socket::INET->new(PeerAddr => $remote_host,PeerPort => $remote_port,Proto => 'tcp',Type => SOCK_STREAM) || die "Couldn't connect to $remote_host\:$remote_port : $!\n";
print $socket "This is a test line - hello world\n";
Junior C.
Frequent Advisor

Re: Perl Network Printing Script

Rob,

What printer are you using specifically ?
515 for LPD based printers.

I want to print doc to printer MYPRINTER port 515.

Appreciate your help


JC.
Court Campbell
Honored Contributor

Re: Perl Network Printing Script

There are also printers that support both jetdirect and lpd.
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Court Campbell
Honored Contributor

Re: Perl Network Printing Script

Are you wanting suggestions or are you wanting some one to write the script for you?
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Junior C.
Frequent Advisor

Re: Perl Network Printing Script

Court,

>> Are you wanting suggestions or are you >>wanting some one to write the script for you?


I do not someone to write the script for me. That will defeat the whole purpose of me learning(well trying)to learn perl.

Suggestion, example, will be appreciated.


JC.
Junior C.
Frequent Advisor

Re: Perl Network Printing Script

All,

I did make some progress, well I can print doc to my network printer.

Following are my script.

I'm not sure how to print heading.

use strict;
use vars '@ARGV';
use Net::LPR;
use IO::File;

#if (scalar(@ARGV) < 2){die "\\nUsage: $0 \n";};

my $var0 = "myfile.txt";
my $var1 = 'printer';

my $lp = new Net::LPR(
StrictRFCPorts => 0,
RemoteServer => $var1,
RemotePort => 515,
PrintErrors => 0,
RaiseErrors => 0,
) || die "Can't create print context\n";

my $fh = new IO::File $var0, O_RDONLY or die "Can't open $var0: $!\n";
#my $fh = new IO::File $ARGV[0], O_RDONLY or die "Can't $ARGV[0] $!\n";

my $size = ($fh->stat())[7];

$lp->connect() || die "Can't connect to printer $var1: ".$lp->error."\n";

my $jobkey = $lp->new_job() || die "Can't create new job: ".$lp->error."\n";

$lp->send_jobs('lp') || die "Can't send jobs: ".$lp->error."\n";

# Can easily print postscript by changing method to job_mode_postscript
$lp->job_mode_text($jobkey) || die "Can't set job mode to text: ".$lp->error."\n";

$lp->job_send_control_file($jobkey) || die "Can't send control file: ".$lp->error."\n";

$lp->job_send_data($jobkey, '', $size);

while (my $line = $fh->getline())
{
$lp->job_send_data($jobkey, $line);
}

$lp->disconnect();

__END__
Ralph Grothe
Honored Contributor
Solution

Re: Perl Network Printing Script

As Alexander already has shown you
the generic (OO interface) for Internet sockets in Perl would be IO::Socket::INET
(if you are more used to the procedural BSD Socket API you could also use the Perl Socket module, but this requires more function calls and awkward address translations).
However, this leaves the burden of compliance to the application's protocol to you.
So, fine that you found a more specialized module like Net::LPR.
I have to admit that I have never used it,
but I guess you found your way by its POD.
Maybe just a few "hints"?
There is no need to "use vars" on @ARGV
as this is implicitly a package global.
Besides, "use vars" is obsolete, and globals should be declared by "our" instead.
Also there is no need to scalar @ARGV if Perl can find out the ("Do What I Mean") context by itself.
So "if (@ARGV < 2)" should suffice.
But it never hurts to be explicit.
As you aren't using much functionality of IO::File (or those inherited from IO::Handle)
it seems a bit wasteful.
For instance to get the size of a file this would suffice: "my $size = -s $fh" (see perldoc -f -s).
So you don't even need to call stat().
Instead of instatiating an IO::File object
you could use a simple open() of the file whose content is to be printed.
If you passed that (or more) file(s) as arguments along with your Perl script invocation some further magic happens,
in that the file is implicitly opened and its name temporarily stored in $ARGV.
Then by assigning the buffer variable in the read loop to "<>" you can even omit the getline() call.




Madness, thy name is system administration
Junior C.
Frequent Advisor

Re: Perl Network Printing Script

Ralph,

I appreciate your feedback/pointers.


Thread Close.


JC.