LittleTreeX
LittleTreeX

Reputation: 1239

How to open an application via php and perl?

I am trying to print generated forms / receipts through PHP (the printers will be installed on the server, I am not attempting to print to a user's local printer). I have decided to try the following methodology:

IN PHP:

  1. Generate a PDF file and save it on the server.
  2. Call a perl script to print said PDF file.

IN perl:

  1. Use system() to "open" Reader and print the given PDF silently.

What works:

What doesn't work:

What happens:

What I've tried:

Other info:

I'm at a loss here, and while it seems like this is an Apache / permissions problem, I cannot be sure. My experience with Apache is limited, and most of what I find online is linux commands that don't work in my environment.

Upvotes: 2

Views: 1224

Answers (1)

Chris Reuter
Chris Reuter

Reputation: 1640

Try this:

my @args = ('C:/Program Files (x86)/Adobe/Reader 10.0/Reader/AcroRd32.exe');
if (system(@args) != 0) {
    # Can't run acroread.  Oh Noes!!!
    die "Unable to launch acrobat reader!\n";
}

The thing about system() is that it does two different things depending on the number and type(s) of argument it gets. If the argument is an array or if there are multiple arguments, Perl assumes the first is the program to run with the rest as its arguments and it launches the program itself.

If, however it's just one string, Perl handles it differently. It runs your command-line interpreter (typically CMD.EXE on Windows) on the string and lets it do what it wants with it. This becomes problematic pretty quickly.

Firstly, both Perl and the shell do various kinds of interpolation on the string (e.g. replace '//' with '/', tokenize by space, etc.) and it gets very easy to lose track of what does what. I'm not at all surprised that your command doesn't work--there are just so many things that can go wrong.

Secondly, it's hard to know for sure what shell actually gets run on Windows or what changes Perl makes to it first. On Unix, it usually doesn't matter--every shell does more or less the same with simple commands. But on Windows, you could be running raw CMD.EXE, GNU Bash or some intermediate program that provides Unix-shell-like behaviour. And since there are several different ports of Perl to Windows, it could well change if you switch.

But if you use the array form, it all stays in Perl and nothing else happens under the hood.

By the way, the documentation for system() and $? can be found here and here. It's well worth reading.

Upvotes: 1

Related Questions