For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2005 > Send file to printer









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Send file to printer
Brian Volk

2005-03-24, 8:56 am

Hi All,

I'm trying to send the output file to a network printer. The program is
working fine as far as producing the file, but it is not being sent to the
printer. Can someone pls take a look at my program and see if everything
looks Okay. It may just be the path to the printer, not sure. Thanks in
advance!

--- begin

#!/usr/local/bin/perl

use strict;
use warnings;
use File::Copy;

my $source_folder = "C:/brian/text_files";

my $dest_file = "C:/brian/test/item_file1.txt";
open ITEMFILE, ">$dest_file";

# open dir and read the files

opendir(FILES,$source_folder);
my @files_to_process = grep {!(/^\./) && -f "$source_folder/$_"}
readdir(FILES);
closedir (FILES);

foreach my $source_file(@files_to_process) {


print ITEMFILE "$source_file\n";
copy ($source_file, '//HP-EXCH/HP LaserJet 4100(IS)');
}

close ITEMFILE;

--- end

Brian Volk
Brian Volk
HP Products
317.298.9950 x1245
<mailto:bvolk@hpproducts.com> bvolk@hpproducts.com



Offer Kaye

2005-03-24, 8:56 am

On Wed, 23 Mar 2005 16:05:53 -0500, Brian Volk wrote:
> Hi All,
>
> I'm trying to send the output file to a network printer. The program is
> working fine as far as producing the file, but it is not being sent to the
> printer. Can someone pls take a look at my program and see if everything
> looks Okay. It may just be the path to the printer, not sure. Thanks in
> advance!
>
> copy ($source_file, '//HP-EXCH/HP LaserJet 4100(IS)');


What makes you think that File::Copy's "copy" supports printing
directly to a printer this way? According to "perldoc File::Copy", if
the argument is a string, it is assumed to be the name of a file. This
is not the case here, so I doubt this will work...

I suggest either using the Net::Printer module
(http://search.cpan.org/dist/Net-Printer/) or manually opening a
filehandle to an "lp" process and providing that filehandle as the
argument to "copy".

Hope this helps,
--
Offer Kaye
Brian Volk

2005-03-24, 8:56 pm

>
> On Wed, 23 Mar 2005 16:05:53 -0500, Brian Volk wrote:
> The program is
> being sent to the
> if everything
> sure. Thanks in
> 4100(IS)');
>
> What makes you think that File::Copy's "copy" supports printing
> directly to a printer this way? According to "perldoc File::Copy", if
> the argument is a string, it is assumed to be the name of a file. This
> is not the case here, so I doubt this will work...
>
> I suggest either using the Net::Printer module
> (http://search.cpan.org/dist/Net-Printer/) or manually opening a
> filehandle to an "lp" process and providing that filehandle as the
> argument to "copy".
>
> Hope this helps,
> --
> Offer Kaye



Thank you for the response, I will look into using Net::Printer.

To answer your question. "What makes you think that File::Copy's "copy"
supports printing
directly to a printer this way?" I was google'ing and found this....

---------------- Google "perlmonks.thepen.com/158993.html"
-------------------
Answer: How do I send a file to the printer on Win32? contributed by munchie
It's very easy, as I recently discovered.

use File::Copy;

copy($file,'lpt1:');



Answer: How do I send a file to the printer on Win32? contributed by
Anonymous Monk
There is also another trick to write directly to a named printer, assuming
the printer is shared and using the UNC syntax:

copy($file,'//host/printername');
----------------------------------------------------------------------------
--

Thanks!

Brian Volk
Offer Kaye

2005-03-24, 8:56 pm

On Thu, 24 Mar 2005 08:11:42 -0500, Brian Volk wrote:
>
> To answer your question. "What makes you think that File::Copy's "copy"
> supports printing
> directly to a printer this way?" I was google'ing and found this....
>
> Google "perlmonks.thepen.com/158993.html"


Well, that looks like your code and since I don't really know anything
about printing from Perl on a Windows network I can't say what's
wrong. Did you check the last sentance:
"assuming the printer is shared and using the UNC syntax"

Another guess - maybe the problem is the whitespaces in the printer
name. Try escaping them using backslashes:
copy ($source_file, '//HP-EXCH/HP\ LaserJet\ 4100(IS)');

I would also try changing the single quotes to double quotes.

Hope this helps,
--
Offer Kaye
Brian Volk

2005-03-24, 8:56 pm


>
> Another guess - maybe the problem is the whitespaces in the printer
> name. Try escaping them using backslashes:
> copy ($source_file, '//HP-EXCH/HP\ LaserJet\ 4100(IS)');
>
> I would also try changing the single quotes to double quotes.
>
> Hope this helps,
> --
> Offer Kaye
>


I tried that too.. :~) I'm now working w/ the Net::Printer module and I
think I'm getting closer. The error I'm getting is below.. Not too sure
what Unknow error means.... but I'll keep searching.

Thanks for your help.

-- begin

C:\brian\Perl>perl send_to_printer.pl
Use of uninitialized value in sprintf at C:/Perl/site/lib/Net/Printer.pm
line 742.
Couldn't send data: Unknown error at send_to_printer.pl line 39
ERROR:Net::Printer[168]: Error Occured sending data to printer
at send_to_printer.pl line 39

#!/usr/local/bin/perl

use strict;
use warnings;
# use File::Copy;
use Net::Printer;

my $source_folder = "C:/brian/text_files";

my $dest_file = "C:/brian/test/item_file1.txt";
open ITEMFILE, ">$dest_file";

opendir(FILES,$source_folder);
my @files_to_process_list = grep {!(/^\./) && -f "$source_folder/$_"}
readdir(FILES);
closedir (FILES);

foreach my $source_file(@files_to_process_list) {

print ITEMFILE "$source_file\n";

my $lineprinter = new Net::Printer (
filename => "C:/brian/test/item_file1.txt",
printer => "HP4100-IS", # this is the Share name
server => "HP-EXCH",
port => "515"'
#lineconvert => "NO"

);


my $result = $lineprinter->printfile();

}

-- end

Brian Volk
Luke Bakken

2005-03-24, 8:56 pm

> > Another guess - maybe the problem is the whitespaces in the printer
>=20
> I tried that too.. :~) I'm now working w/ the Net::Printer=20
> module and I
> think I'm getting closer. The error I'm getting is below.. =20
> Not too sure
> what Unknow error means.... but I'll keep searching.
>=20
> Thanks for your help.
>=20
> -- begin


c:\>net use lpt1: "\\hp-exch\HP LaserJet 4100(IS)" /p:n

Then from the command line:

c:\>copy file_for_printer lpt1:

If that works, then using File::Copy to lpt1: should work as well.

Luke
Brian Volk

2005-03-24, 8:56 pm



>From: Bakken, Luke [mailto:Luke.Bakken@getronics.com]


> c:\>net use lpt1: "\\hp-exch\HP LaserJet 4100(IS)" /p:n
>
> Then from the command line:
>
> c:\>copy file_for_printer lpt1:
>
> If that works, then using File::Copy to lpt1: should work as well.
>
> Luke
>


Thanks Luke! When I type this at a command line I get "The network
resource type is not correct".

net use lpt1: "\\hp-exch\HP LaserJet 4100(IS)" /p:n

Any ideas what that means?

Thanks!

Brian


Brian Volk

2005-03-29, 8:56 pm



> -----Original Message-----
> From: Brian Volk [mailto:BVolk@HPProducts.com]
> Sent: Thursday, March 24, 2005 11:24 AM
> To: 'Bakken, Luke'; 'Perl Beginners'
> Subject: RE: Send file to printer
>
>
>
>
>
>
> Thanks Luke! When I type this at a command line I get "The network
> resource type is not correct".
>
> net use lpt1: "\\hp-exch\HP LaserJet 4100(IS)" /p:n
>
> Any ideas what that means?
>
> Thanks!
>
> Brian



Hi All,

I was able to send the file to the printer using: copy ($print_file,
'//hp-exch/HP4100-IS');
just like the documetation said .. :~) I just had to get the server name
correct!

Thanks!

Brian








Brian Volk

2005-03-29, 8:56 pm


>
> Hi All,
>
> I was able to send the file to the printer using: copy
> ($print_file, '//hp-exch/HP4100-IS');
> just like the documentation said .. :~) I just had to get
> the server name correct!
>
> Thanks!
>
> Brian
>
>
>



Well, I thought I knew what I was doing. Now I am having a problem w/ the
carriage return. When the file prints to the screen, it looks fine..
however, when it prints to the printer, I does not see the carriage return
and only prints the first line. I have tried inserting "\n" will no luck..
Can someone pls help.

Thanks!

---begin

#!/usr/local/bin/perl

use strict;
use warnings;
use File::Copy;

my $print_file = "C:/brian/test/12345.txt";
open (PRINT, "< $print_file") or die "can't open $print_file : $!";

while( <PRINT> ) {
print;

}

copy ($print_file, '//hp-exch/HP4100-IS');

close PRINT;

---end

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com