Php – Batch file that opens adobe reader to print PDF doesn’t end/close

batch-filephpshell-exec

I have the following code in a batch file that is called by a PHP script using shell_exec():

"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe" 
/t "D:\xampp\htdocs\instrument\app\webroot\Repair Tickets\%1.pdf"
\\hnurenfp01\Accounts_FS-1128MFP

Parameter %1 is the filename that is sent as an argument to the batch file from the PHP script.

Observed behaviour:

  • PHP page successfully calls batch file with correct argument
  • Two instances of AcroRd32.exe silently start on the server
  • PDF file prints to network printer
  • AcroRd32.exe instances do not end/close
  • Batch file keeps running (until manually ending AcroRd32.exe processes)
  • PHP script is left executing, therefore preventing the client machine to do anything further

Expected behaviour:

  • PHP page successfully calls batch file with correct argument
  • Two instances of AcroRd32.exe silently start on the server
  • PDF file prints to network printer
  • AcroRd32.exe instances end/close
  • Batch script completes (which in turn should free up the PHP script)
  • PHP script continues and eventually ending to allow end-users to continue with their work

Failed solutions:

  • Using TASKKILL in the batch file to end AcroRd32.exe processes after print, but AcroRd32.exe remains open, even using the /F (force) flag. When I manually end the AcroRd32.exe processes, the batch file continues and then executes the TASKKILL line, which of course return a "process can't be found" error because it's no longer there. So it would seem the control is not returned back to the batch file after the print, it remains with AcroRd32.exe.

Does anyone have any ideas how I can close Adobe Reader after the PDF prints?

Best Solution

I found the following quote at http://www.robvanderwoude.com/commandlineswitches.php#Acrobat

Print a PDF file silently:

AcroRd32.exe /N /T PdfFile PrinterName [ PrinterDriver [ PrinterPort ] ]
The last command will open a new Adobe Reader window, print the PDF file and then terminate its window unless that window happens to be the only Adobe Reader window left: at least one Adobe Reader window will be left open.

That last sentence is bad news.

You could call AcroRD32.exe with both /N and /T options via the START command, and then your batch file can continue on immediately while the print job executes. If you don't mind one instance of the reader remaining open, then you are done.

If you must close the reader, then your batch will have to kill it. But I'm not sure how your batch file can tell when the print job has finished. You could introduce an arbitrary delay using TIMEOUT, but that sounds risky.

I'm a bit confused about the "two instances of AcroRd32.exe" being opened. If that is the way the program works, then it sounds like the /N switch will close 1 of the 2 instances. Your batch could launch the print job via START, delay a couple seconds to give the print job processes time to start, and then use TASKLIST|FINDSTR to monitor the number of AcroRd32.exe processes in a loop. When the count goes from 2 to 1, the print job should be complete and you can kill the remaining process. In theory anyway :)

Good luck.

Related Question