Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Print a text file to a Windows network printer in a landscape orientation.

by iskinner (Novice)
on Apr 04, 2008 at 21:28 UTC ( #678462=perlquestion: print w/replies, xml ) Need Help??
iskinner has asked for the wisdom of the Perl Monks concerning the following question:

I believe I am nearing the end of month long porting of a rather extensive, 1500 line, 15 year old Perl application that used to run on one of our old Solaris Unix boxes to running on our new Windows 2003 box.

I believe the last obstacle to over come is to print a text file to a network printer in a landscape orientation.

After much searching I have found the Win32::Printer and Win32::Printer::Direct modules. But I'm not sure how to make use of them to achieve my goal.

Creating an instance of Win32::Printer, I can supposedly tell it to print in landscape.

use Win32::Printer; $foo = new Win32::Printer( orientation => LANDSCAPE );

With Win32::Printer::Direct, I can easily print the text file.

use Win32::Printer::Direct; Printfile('\\\\hardcoyp\\ApDev','test.txt');

How the halibut can I combine these features? Assuredly there has got to be a way, but what it is I can not fathom.


My experience with Perl is a bit stale dating from the late 90's. And then I was just a novice making simple modifications to canned CGI scripts that sent e-mails from form submits. I do not think I understand modules well enough to know how to get the more specific 'Direct' module to use features of the more general 'Printer'? I don't even understand enough to know if that is a sensible question to ask.


Replies are listed 'Best First'.
Re: Print a text file to a Windows network printer in a landscape orientation.
by mr_mischief (Monsignor) on Apr 04, 2008 at 21:55 UTC
    It's absolutely a sensible question to ask.

    Win32::Printer::Direct is actually a wrapper around functionality provided by Win32::Printer itself. The details of the call it makes don't really appear in the POD nor do they appear in the Perl source of the Win32::Printer module. The call is either, then, in the XS part of the module or a call into the Windows API. I haven't tracked down for you which. The Windows API reference could probably come in handy if you're dealing with this module issue.

    There are other ways to handle printing that might be a little more general. If you can expect all of your users to have a PDF reader such as Acrobat Reader or FoxIt, then you could use something based on PDF::API2 and have the PDF reader program handle printing. There's plenty of documentation on that module.

    There are also other document formats that might work for you other than PDF that still get you the printing part for free, but that's the one I have recent experience needing to use for printable material from Perl. PostScript comes to mind, as do ODF and .doc files.

      Quote: There are other ways to handle printing that might be a little more general.

      I am most open to any other options to meet this requirement of printing a page to a Windows network printer in landscape orientation. I have tried to explore several options today such is working from the DOS command line. But I have not found anything that quite gets me to the finish line.

      I am not sure your PDF or related suggestions would work, but if I am mistaken please enlighten me. There is no human interaction with the process once it is up and running. The application watches a directory during the quite night time hours. When if finds files are loaded there, it starts extensive processing of them resulting in many many database transactions and several text files.

      One of these text files is sent to a printer where, the following morning, somebody grabs the resulting pages from the output tray, stuffs them into envelopes and mails them all over the State of California.

      If anybody can suggest alternatives to the Win32::Printer{::Direct} option, I am all eyes.

      Thank You

        The only other library I know of that lets you print from windows is Wx. There's a nice example in the wdperl_demo.

        You might be able to do something with Word (or perhaps another app) through OLE using Win32::OLE if you know OLE. I don't.

        TGI says moo

        An easy workaround (although not very general) might leverage what type of networked printer you have. A JetDirect print server generally has FTP through which you can transfer any type of file (plain text, PCL, PostScript, raw) the printer attached to it can print. I'm not sure how to specify landscape printing other than at the printer then, though. A PostScript file can specify the orientation of its contents.

        If you're using a networked printer through the UNC path from Windows, you can probably control a PDF or ODF-compatible application on the server via OLE to do the printing. Whether that'd be easier than figuring out the detailed layout methods of Win32::Printer or getting Win32::Printer::Direct to use Win32::Printer's landscape mode I couldn't say.

        In the vein of using the DOS "print" command, that works with your server's print driver IIRC, so you should be able to set all of your printers (if you have more than one) to landscape on the server. Unfortunately, portrait v. landscape settings in the driver never seem to have worked with that command. It lets you specify which printer device to print to, including UNC paths. print /d:\\unc\path filename should work. It's easy enough to share a printer with a name "foo" on the server with a name of "bar", then shell out to print /d:\\bar\foo datafile. If it did landscape, you'd really be in luck.

        If you can get the file to an lpd somewhere from the Windows box, that might be worth investigating as there's an lpr for Windows (probably several). There are also lpd servers that run on Windows, for that matter.

        By using a PostScript-compatible printer, a PostScript document that sets its own orientation, and either the CLI-based print command or an lpr/lpd combo, you might have a solution that way.

Re: Print a text file to a Windows network printer in a landscape orientation.
by bkiahg (Pilgrim) on Nov 04, 2008 at 02:57 UTC
    Not sure if you ever found a work around for this but here is the code that would probably work using Win32::Printer
    use Win32::Printer; my $dc = new Win32::Printer( printer => '\\\\hardcopy\\ApDev', orientation => LANDSCAPE ); my $filename = 'test.txt'; my $image_handle = $dc->Image($filename); $dc->Image($image_handle); $dc->End(); $dc->Close();
    Disclaimer: Untested code and I'm currently in the process of trying to figure this thing out myself, but that should work above.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://678462]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-01-23 08:26 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (241 votes). Check out past polls.