merrymonk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I need to print directly to a printer connected to my PC.
Somewhere on your site I found information that led me to believe
this is done by a command of
open(PRN, ">LPT1") - if the printer is connected to LPT1 of the PC being used
or open(PRN, '>\\printserver\printershare') if the printer is
connected to a printer on the network.
Therefore I wrote the follwing code

use strict "vars"; use Win32; my ($port_name, $port_name_use, $res_file_open, $page_item); print "\ngive port name for Zebra printer (eg LPT1/USB001) "; $port_name = <STDIN>; chomp($port_name); $port_name_use = '>' . $port_name; print "port name to be used is $port_name_use\n"; $res_file_open = open(PRN, $port_name_use) ; if($res_file_open == 1) { $page_item = 0; while ($page_item < 3) { $page_item +=1; print PRN "page $page_item\n"; print PRN "line 2\n"; print PRN "line 3\f"; } close(PRN); } else { print "could not open zebra printer on port $port_name\n"; }

Sadly this just treats the name in the open as a file therefore nothing is sent to
the printer.

What changes do I have to do so that I can print directly to the printer connected to my PC?

Replies are listed 'Best First'.
Re: Printing directly to a printer
by gellyfish (Monsignor) on Apr 08, 2005 at 15:02 UTC

    I think you need the colon after the device name (e.g. LPT1:)


      Many thanks for your suggestion which I have now tried
      and variations such as leaving the > off at the beginning
      This did not print but it moved a step forward since the open
      failed. I also tried putting the : at the beginning
      but this was as originally but no file was written to.
      Looking at the port for the printer it describes it as a Virtual Printer Port for USB
      I do not know if this affects what should be done to make it work
      Do you have any more ideas?
Re: Printing directly to a printer
by buckaduck (Chaplain) on Apr 08, 2005 at 22:04 UTC
    I copied/pasted your code as shown here and ran it on my Windows XP. It runs just fine for me. (I typed "LPT1" as my printer port; however, your printer may not be on LPT1.)


      Many thanks for trying it on your PC
      and letting me know what happened.
      I think that the port may be an issue since my printer
      is attached to a USB port which is described as a
      Virtual Printer Port for USB.
      I shall try some more with a printer attached to LPT1