Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to print (on Windows ) from perl

Posted on 2004-11-03
13
Medium Priority
?
186 Views
Last Modified: 2010-04-17
Hi gurus -

I have a program that reads a mysql database and outputs a spreadsheet -

Is there a way to get it to print off to the default printer automatically -

Or maybe write out the file - then get a 2nd program to read the directory and if there is a document , then print it out .....

Can you give me a few pointers ?

thanks in advance .....

    Rab
0
Comment
Question by:rab54
  • 6
  • 5
12 Comments
 
LVL 19

Expert Comment

by:RanjeetRain
ID: 12483104
Which spreadsheet format are you going to output it to? Of course you need to tell us, your OS platform too.

On windows you can simple shell out the document with a '/P' switch. Generally, on Windows platform that switch causes the host application to print it using the default printer.

If your OS/spreadsheet doesn't support that, let us know.
0
 
LVL 19

Expert Comment

by:RanjeetRain
ID: 12483115
Oh my!! Sorry, i see that you wrote "print (on Windows ) from "...

Need galsses????
0
 

Author Comment

by:rab54
ID: 12483273
I have used the system command within perl to print a test file

eg - system "type $file > $printer";

This works ok until I try a spreadsheet - doh !

I just need a dos command to print say -

c:/test.xls

I had no joy with PRINT and /P either .....

cheers
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:RanjeetRain
ID: 12483878
Nope. Try this:

cmd start %1 /p
0
 

Author Comment

by:rab54
ID: 12485303
Can you tell me what this actually does ?

eg 'start' ,%1 /p etc ...

sorry to be numb ...
0
 
LVL 19

Expert Comment

by:RanjeetRain
ID: 12486047
cmd is the command line processor on Windows NT. Its equivalent on Windows 9x is Command.com Its kind of a shell program. It can be used to execute other programs as well.

'Start' is a means to launch an executable file or a document file with the help of an application registered to handle that particular document type.

/P is a switch generally used by Windows applications to quitely print a document.

Hope that helps!
0
 

Author Comment

by:rab54
ID: 12486340
ok i am trying this within my perl program -

system "cmd start perl.exe $file/P";

But still having no joy -

Does the above mean use perl to run the program in $file ?

But $file is a spreadsheet .... not a perl program -

Thanks for the continued help .... I feel like a blindman in the dark ..... ;~)
0
 

Author Comment

by:rab54
ID: 12496627
OK cheers guys -

Got it sorted using Win32::OLE

rab
0
 
LVL 19

Expert Comment

by:RanjeetRain
ID: 12496810
Its good that you found a solution. Can you please post your relevent code for us.
0
 

Author Comment

by:rab54
ID: 12503069
No problem mate -

If you see the code below - it reads in the selected spreadsheet -
we get the default printer using the ActivePrinter method - assign it and bingo !

There is an option to output to another file if needed -

Hope this is of help to others -

cheers

 Rab

ps - Obviously you will need to install the Win32::OLE module first ;~)

################### code #########################
$Win32::OLE::Warn = 3;                                # die on errors...

use Win32::OLE qw(in);
    use Win32::OLE::Const;
    my $Class = "Excel.Application";
   
    my $File  = "path\to_file";
   
    my $Excel = Win32::OLE->GetActiveObject( $Class );
    if (!($Excel))
    {
        $Excel = new Win32::OLE($Class, \&QuitApp ) ||
                 die "Could not cretate an OLE '$Class' object";
    }
    $Excel->{Visible} = 1;
#    my $Book = $Excel->Workbooks->Open( $File ); # open the file
#    my $count = $Book->Worksheets()->{Count};
#    print("There's $count worksheets in $File\n\n");
 
 my $Book = $Excel->Workbooks->Open( $File);
# above line opens the file
print("Opened file $File...\n");
if (!($Book))
{
    print("Error during open, filename=$File. returning 0.");
    return 0;
}

my $count = $Book->Worksheets()->{Count} || 0;

 
my $pobj = $Excel->ActivePrinter;

## check for correct printer
if ($pobj) { print("Name of default printer =>>$pobj<<\n"); }

$Book->ActiveSheet->Activate();
my @thing = $Book->PrintOut(
{
 #   PrintToFile   => 1,
  # PrToFileName  => "$outfile",
  #  PrToFileName  => "$pobj",
    ActivePrinter => "$pobj",
    Copies        => 1
});

################# end code ########################

0
 
LVL 19

Expert Comment

by:RanjeetRain
ID: 12526083
Your code looks okay. Just that it only prints Excel files. For your question it seemed you wanted to use shell to print *ANY* file in a generic way. Anyway, glad for you that you solved your probolem.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12860829
PAQed with points refunded (125)

modulo
Community Support Moderator
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Introduction to Processes

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question