Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How do you open a PDF file from a perl script in Windows?

by hmj6jmh (Initiate)
on Feb 11, 2013 at 22:44 UTC ( #1018255=perlquestion: print w/ replies, xml ) Need Help??
hmj6jmh has asked for the wisdom of the Perl Monks concerning the following question:

This may seem like a silly question, but I have scoured the net and tried many different permutations of using the perl system command to open a PDF I generated in a script, and it doesn't work. Here is the latest permutation:

system("C:\\Program Files\\Adobe\\Acrobat 9.0\\Acrobat\\Acrobat.exe", $outname);

This opens acrobat but not the file itself in acrobat. What is the correct syntax?

Comment on How do you open a PDF file from a perl script in Windows?
Download Code
Re: How do you open a PDF file from a perl script in Windows?
by davido (Archbishop) on Feb 11, 2013 at 22:56 UTC

    Your calling syntax is not incorrect, and the parts that we're seeing are not in disagreement with how Adobe reader is intended to be called. So since we know it's not a problem with the Perl code you've shown, that leaves four questions that I can think of:

    1. What does $outname contain?
    2. What is the cwd for your script.
    3. What is the absolute path to your PDF file?
    4. Does your script have read permission for that PDF?

    You might be specifying a relative path that doesn't lead where you want it to when the script runs. Or your script might not have the correct permissions for the PDF.


    Dave

Re: How do you open a PDF file from a perl script in Windows?
by NetWallah (Abbot) on Feb 12, 2013 at 00:05 UTC
    If the ".pdf" suffix is associated with Adobe, you can simply pass the file name to "system", and it will do the right thing. This works for me (Windows7):
    perl -e "system q|myfile.pdf|"

                 Most people believe that if it ain't broke, don't fix it.
            Engineers believe that if it ain't broke, it doesn't have enough features yet.

Re: How do you open a PDF file from a perl script in Windows?
by hmj6jmh (Initiate) on Feb 12, 2013 at 01:25 UTC
    Wow, that was fast. Thanks. All good questions. OK, here's what I found when I looked at how I created $outfile:
    $0 =~ /(.*)\.pl/; my $outname = "$1.pdf";
    This was in a subroutine so $outfile was local. I then tried to use it outside the subroutine so that was the problem. I removed the my and now the above call works. I can also invoke just the name and that works too:
    system $outname;
    I tried this with exec and I get:
    There was an error opening this document. This file cannot be found.
    and then it opens when I 'OK' the dialog. What is the syntax for exec? I though it was the same.

      I'm glad you got it working, but this line haunts me:

      This was in a subroutine so $outfile was local. I then tried to use it outside the subroutine so that was the problem. I removed the my and now the above call works.

      What that tells me is that you aren't using strict, and that you are now creating variables inside of subroutines that you are accessing outside of the subroutines. A better approach would be to let your subroutine return a value that the calling code uses to initialize $outname


      Dave

        Yes, of course, if I had used strict and warnings, this would have been avoided. I know I am following Perl Worst Practices. I will reform my undisciplined coding ways. Thanks, oh brother sufferer, for the admonition. ;) Meanwhile, why is exec behaving so?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1018255]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-09-23 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (241 votes), past polls