View Full Version : Using exec() to run .exe files.

03-19-2008, 10:35 AM
I'm using FPDF to create PDF files what I'd like to do is have the user print them automatically. I want to run acrobat reader and usind the command line
"AcroRd32.exe /t \wamp\www\Test\test.pdf" which will print the pdf to the default printer. This works if I run it from the dos command line.

I've installed arcroabt reader on the server and would like to use the exec() funtion to run the command to print the pdf. What would be the best way to do this?

Using: exec("\wamp\www\Test\Reader\AcroRd32.exe /t \wamp\www\Test\test.pdf");

I get the php page but the file is not sent to the default printer. I gather this is because it is being sent to the server not the desktop.

Any suggstions?

03-19-2008, 12:42 PM
this cant be done without launching a dialogue

this offers the user a choice of where to send the print instruction as they may have multiple printers

03-19-2008, 01:46 PM
Thanks davidj,

After reading through some of the posts in exec() manual I discovered this:

Right click "Apache...", select properties.
Click on the "LOG ON" tab
Check the box "Allow this service to interact with desktop"
Click OK
Restart Apache

Also added cmd.exe /c

so now I have:

exec("cmd.exe /c C:\wamp\www\Test\Reader\AcroRd32.exe /t C:\wamp\www\Test\test.pdf");

It's almost working.

Interesting you say "this cant be done without launching a dialogue" is this a PHP limitation?

It does work when I run cmd.

With regards to the " Check the box "Allow this service to interact with desktop"" mentioned above is there a similar setup/process for the Mac? Have not tried it yet.

03-19-2008, 05:07 PM
its working on your machine...

not the users!

03-23-2008, 01:48 PM
OK a quick update on this. I found another way around what I wanted to do which means I do not have to use exec(). I found that fpdf has a number of great addons. Unfortunatly I'm also using fpdi which is based on fpdf. There's a acrobat JS addon that allow you to include document level JS when you create your pdfs. The example uses the acrobat JS print() which if you set to flase will auto print the page when you open the pdf. I combined the addon into the fpdi and now all the pdfs create have the JS included and when the pdf is opened it auto prints to your default printer. The next part will be to create a popup window that opens the pdf in a browser window and then closes. If I'm correct it should print when the popup opens. Will let you know if this works.

03-24-2008, 10:40 AM
I've hit a slight problem.

All has gone well with this approach the only problem is that when I open the pdf in a browser window I loses control of that window (is this correct?). I've found that if the popup window that open the pdf uses Acrobat not Acrobat Reader you get a confirmation message to print the pdf. If the window uses Acrobat Reader the pdf automatically prints to the desktops default printer.

Can anyone tell me if there is a way I might be able to close the pdf browser window after a set time of 5 sec?

I'm using window.open to open the fdpi page then redirecting to the pdf with either the

header("Location: $printfile ");


echo '<META HTTP-EQUIV="refresh" CONTENT="1; URL="'$printfile'>';

Any suggestions would be appreciated.

03-24-2008, 01:26 PM
I've hit a slight problem.

All has gone well with this approach the only problem is that when I open the pdf in a browser window I loses control of that window (is this correct?). It depends. I think it's a local setting. PDF files never load in a browser window for me.

03-24-2008, 11:16 PM
Hi domedia,

If you have acrobat reader installed on your system with the default install settings any pdf that you open with the browser should open in a browser window but with a acrobat reader shell. There are acrobat preferences that allow you to stop acrobat from opening in a browser window and it will open in the actual app.

After a bit of searching it seems I may need to go back to my exec() approach. I'm currently searching on what options I have to print from php. Does anyone know if there is a way to get php to print to a desktop or network printer?

03-25-2008, 04:05 AM
Nearly there.


<body onload="window.open('<?php echo $printfile; ?>')">I pop open the pdf file in a new window. Now the acrobat auto print works but the close function seems to be blocked in the browser window.

The window with the pdf has a URL and I was hoping someone might be able to tell me if there's a way to close a URL either in php or JS.

03-26-2008, 12:19 PM
After a few days of back and forth I discovered using Javascript to run command line approach would be the best way.

So I'm using:

<SCRIPT type="text/javascript" LANGUAGE="JavaScript">
function executeCommands(inputparms) {
// Instantiate the Shell object and invoke its execute method.

var oShell = new ActiveXObject("Shell.Application");

var commandtoRun = "PATH TO APPLICATION HERE";
if (inputparms != "")

// Invoke the execute method.

oShell.ShellExecute(commandtoRun, commandParms, "", "open", "1");
<body onload="javascript:executeCommands(' + hasPARMS + ');">
To make this work the path/folder on the websever that has the pdf needs to be shared out. I was able to do this on the Mac but having problems on the PC. Using wamp can anyone guide me in the right directions to get this working?

03-27-2008, 10:35 AM
Finally worked out the javascript code to open the fpdf in a popup that will create the pdf then open the pdf just created (with JS print() at doc level) in another popup and then close the pdf popup delete the pdf just created and then close the fpdf page.

<SCRIPT type="text/javascript" LANGUAGE="JavaScript">
//open the pdf just created
var anotherwindow=window.open('<?php echo $printfile; ?>', 'PDF','width=640,height=800,menubar,resizable');

//set an 8 second refresh then delete the pdf
if (!anotherwindow.closed){
function poppdf(){

This works better as it is not restricted to just windows and does not need to have a shared folder on web server so that command line approach can access the pdfs.