Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print to printer on UNIX

Status
Not open for further replies.

alsd

Technical User
Feb 23, 2000
4
HK
Hi all,<br>

<br>

I need some help on this. How do I print to an UNIX printer from a PERL program?<br>

<br>

My thinking is just open the printer as if it is a file with open(), then print
 
I can think of a couple of ways, one I've tried, one I haven't (yet... :).<br>
<br>
First method...<br>
<FONT FACE=monospace><br>
open (MYFILE, &quot;&gt;/tmp/myfile.txt&quot;);<br>
.<br>
... Do some perl stuff writing to MYFILE.<br>
.<br>
close(MYFILE);<br>
system(&quot;lp -dmyprinter /tmp/myfile.txt&quot;);<br>
unlink(&quot;/tmp/myfile.txt&quot;);<br>
</font><br>
Second method, that you suggested, which I haven't tried yet, but that should work a treat...<br>
<FONT FACE=monospace><br>
# Use -s to silence lp.<br>
open(MYPRINTER, &quot;¦ lp -s -dmyprinter&quot;);<br>
<br>
.<br>
... do some perl stuff writing to MYPRINTER.<br>
.<br>
<br>
# If this works as I suspect, print job should start now...<br>
close(MYPRINTER);<br>
</font><br>
2nd way is a lot neater - wish I'd thought of it myself a long time ago! :)<br>
<br>
OK, I'm off to try this now - I'll let you know how I get on :)
 
Works well - as in the following example:<br>
<FONT FACE=monospace><br>
#!/usr/bin/perl<br>
<br>
opendir(HOME, $ENV{'HOME'});<br>
<br>
open(PRINTER, &quot;¦ lp -s -dmyprinter&quot;);<br>
<br>
while ( $file = readdir(HOME) ) {<br>
print PRINTER $file, &quot;\n&quot;;<br>
}<br>
<br>
close(PRINTER);<br>
<br>
closedir(HOME);<br>
</font><br>
<br>
Just replace the <FONT FACE=monospace>lp</font> command with something appropriate for your system.<br>
<br>
HTH.
 
Andy's second suggestion is the way we do things - works fine unless you want to keep a copy of stuff you've printed.<br>
<br>
Mike<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Follow up with old thread

Couple of years back posted a thread in tek-tips ..regarding
printing in perl .
thread219-7086
i tried the same thing ...but its not working for me ..I really
appreciate if you could tell me what i am doing wrong.

Here is the code i used to
$variable_to_print=&quot;abasdhajsd&quot;;

open(PRINTER,&quot;¦lpr -PPrintServer01&quot;) || die &quot;Can't open lpr: $!\n&quot;;
print PRINTER $variable_to_print;
close(PRINTER);

Printer didn't respond to this code.....

when i changed the code to write to a file and then print it ..it
worked ....

open(PRINTER,&quot;>/tmp/temp.txt&quot;);
print PRINTER $variable_to_print;
close(PRITNER);
system(&quot;lpr -PPrintServer01 /tmp/temp.txt&quot;);
unlink(&quot;/tmp/temp.txt&quot;);

I don't want to write to a file as it contains sensitive information .

Thanks .

Regards,
Thendal
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top