2

So, I have this printer that doesn't have drivers for any OS other than Windows, so, I wanted to run a server on a Windows PC that is attached to the printer, so that I can upload a file from any device in the local network and have a PHP script invoke MS Word on the file to print it. The command I have to invoke Word to print a document works when I give it from the command line and the file upload works, but when the command is run from the PHP script with system() or exec(), it does not work. I see WINWORD.exe running from the Task Manager, but with no GUI showing and no printing happening. I am running the latest WAMP on Windows 8 and I have tried going into services.msc and changing the logon for wampapache to my user which is an administrator or enabling the checkbox to allow the service to interact with the desktop, but none of that worked. This is the actual command I am using: "C:\Program Files\Microsoft Office\Office14\WINWORD.EXE" C:\path\file.txt /q /n /mFilePrintDefault /mFileExit. It's in a batch file that I run using exec()

EDIT: I am not trying to print to a network printer, this is a printer that is connected to the server computer and I am using the newest version of WAMP, so I can't use the PHP functions in the linked question. Also, I am trying to print Word documents not raw text. This is specifically about PHP's exec() not working for this case.

dramzy
  • 1,379
  • 1
  • 11
  • 25
  • possible duplicate of [Print directly to network printer using php](http://stackoverflow.com/questions/4956143/print-directly-to-network-printer-using-php) – paulsm4 Sep 24 '15 at 00:25
  • No, read the edit. This is specifically about a problem with PHP `exec()` invoking MS Word on a document to print it. – dramzy Sep 24 '15 at 00:28
  • No, you don't need to explicitly invoke winword.exe with a "system()" call just to print a document. PHP printer_open() can work (with Apache/PHP running on Windows); .Net/MS-Office Interop can also work: http://www.sitepoint.com/make-microsoft-word-documents-php/ – paulsm4 Sep 24 '15 at 00:37
  • Does that work in the latest version of PHP? I read somewhere it does not. – dramzy Sep 24 '15 at 00:41
  • Have you actually had success with any of these methods? I can't see how I can specify a word document with the plain `printer_open()` approach and I tried to follow the Interop approach from your link, but the files specified are very different in Win 8. I prefer this approach. – dramzy Sep 24 '15 at 01:19

1 Answers1

2

I honestly think this is your best bet:

Since you seem to be having problems with the .bat file approach, I'd suggest trying a Powershell script instead. For example:

print_doc.ps1 =>

Param([string] $filePath)
# This should handle any file type, including an MS-Word .doc/.docx
Start-Process -FilePath $filePath -Verb print  

In PHP, you can exec() the script something like this:

'powershell.exe -noprofile -executionpolicy bypass -file \path\to\script\print_doc.ps1 "' . $path . '"'

STRONG SUGGESTIONS:

  1. Make sure the path is valid INSIDE OF PHP.

    You can use file_exists() and/or realpath() to verify this.

  2. Enable verbose error logging. In Powershell, I like to use try/catch blocks and the $error object.

Good luck!

dramzy
  • 1,379
  • 1
  • 11
  • 25
paulsm4
  • 114,292
  • 17
  • 138
  • 190