Using PHP shell_exec to print PDF on the network automatically to a network printer not working

I'm using shell_exec() to print a PDF that is stored on a LAN automatically to a printer on the LAN through Adobe Reader using the following PHP code (all on one line):

shell_exec("'C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe' 
/t \\hnurensp01\RepairTickets\179694.pdf \\hnurenfp01\Accounts_FS-1128MFP");

What I expect this to do is open adobe reader, open the PDF on the network and print to the network printer. When running the PHP script with this line, it doesn't open adobe reader or print it. However, when I copy that code and execute in a command prompt (client-side), it works and prints. There is only slight modifications to the command line (all on one line):

"C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe"
/t \\hnurensp01\RepairTickets\179694.pdf \\hnurenfp01\Accounts_FS-1128MFP

Is it to do with the PHP script executing on the server-side? I've tried running the same command line on the server and it works too. It just seems to be when it's going through PHP, it does nothing.

My end result must be printing out a server-side PDF with as little interaction from the end-user as possible. Note: this is a WAMP environment.

Any ideas?


UPDATE: Tried put the code into a batch file and calling it that way, still didn't work. Tried using exec() and shell_exec() but still no joy. For some reason when I run the batch file by double-clicking it, it works fine. It's when I call it using PHP, it doesn't do anything except open two instances of acrord32.exe that doesn't close and therefore the PHP script seems to never complete. I'm soo confused...


Try this:

echo "'C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe' /t \\hnurensp01\RepairTickets\179694.pdf \\hnurenfp01\Accounts_FS-1128MFP"

It returns:

'C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe' /t \hnurensp01\RepairTickets9694.pdf \hnurenfp01\Accounts_FS-1128MFP

In PHP, the backslash serves as a 'special' character, since it's used to produce tabs and newlines \t and \n. The proper way to escape a backslash, is by prepending another one: \\ produces \ as output.

This is why your PHP script doesn't work. The double backslashes will be printed to the shell as if you only typed one! You will need to escape every backslash in that line of code:

shell_exec("'C:\\Program Files\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe' 
    /t \\\\hnurensp01\\RepairTickets\\179694.pdf \\\\hnurenfp01\\Accounts_FS-1128MFP");

Need Your Help

How to create table defined in sqlalchemy schema in the Heroku server?

python heroku sqlalchemy database-schema turbogears2

I have a very simple app that I'm building with Python, SQLAlchemy, PostgreSQL and Turbogears 2.3.

cloudformation best practices in AWS

amazon-cloudformation cloud-init

We are at early stages with running our services on AWS. We have our server hosted in AWS, in a VPC, having private and public subnets and have multiple instances in private and public subnets usin...