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.
I honestly think this is your best bet:
How To Make Microsoft Word Documents with PHP
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:
Make sure the path is valid INSIDE OF PHP.
You can use file_exists() and/or realpath() to verify this.
Enable verbose error logging. In Powershell, I like to use try/catch blocks and the $error object.
Good luck!
Related
I would like to use the wkhtmltopdf for HTML to PDF conversion.
When I have tried to convert it via linux terminal, it works fine.
But when I have tried with the php script it does not work.
I am trying execute the binary directly.
here is the code I am trying with PHP.
exec('/home/binary_loc/wkhtmltopdf http://www.google.com /home/user/output.pdf');
My binary is at the same folder where "index.php" exist.
I have tried to fetch the version of wkhtmltopdf binary with PHP, then it return the version.
But i don't able to understand why not it work to execute with php for pdf.
Here is code for version check using php.
error_reporting(E_ALL);
ini_set('display_errors', '1');
$cmd = "./wkhtmltopdf --version";
$t = shell_exec($cmd);
echo $t;
exit()
Do anyone has solution regarding it??
I want this because this will work in the shared hosting too. No need to install the wkhtmltopdf in the server.
Exec probably doesn't have permission to execute the file. PHP usually runs as either apache or nobody, rather than your user account. You'll have to make sure the execute bit is set for whatever user it runs under. You can use chmod 755 wkhtmltopdf from the directory it's under, to grant read and execute to all users.
Note that on some shared hosting, exec() is disabled. Check with your host to make sure you have access to it.
Define the complete path to the wkhtmltopdf executable and the complete path to the output folder.
To debug try something like this:
shell_exec("/home/binary_loc/wkhtmltopdf http://www.google.com /home/user/output.pdf > /home/user/debug.log 2>&1");
pipe the error in a file
I am running a local WAMP on my Windows 7 with a PHP script that executes a windows command as follows:
`exec('"%CD%\files_for_redistribution\ppt2html5.exe" /i:"%CD%\test.ppt" /o:"%CD%\output.html" /title:title /desc:description /author:author /keywords:keywords',$output,$error);`
The command when run from a batch file does the job well but when run from PHP script, gives an error:Presentation opening error: PowerPoint could not open the file.
The intention of the command is to convert PowerPoint to HTML using a third party software called ppt2html5.exe where test.ppt has to be converted to output.html.
I have found lot of blogs discussing about exec function not working properly but nothing really helped me to deal with this error as it runs the command but cannot open the file.
It would be great if somebody could help me with this.
Check if safe mode is on, because that activates escapeshellcmd and some characters are escaped.
Assuming that the string that you are passing to exec(), including percentage signs, routes and parameters are right, your problem may be related to permission of files and user executing apache + php, check that.
Fixed by adding a folder named Desktop inside C:\Windows\System32\config\systemprofile.
Source:http://www.sitepoint.com/forums/showthread.php?956457-Windows-2008-PHP-new-COM%28powerpoint-application%29
I've installed the LibreOffice RPMS (have tried both 3.5.3 and 3.4.6) on my CentOS machine, and I'm trying to make PHP run a shell script that calls LibreOffice. In the shell script, it runs executes this line:
/opt/libreoffice3.4/program/soffice --headless -convert-to $extension.pdf "$1" -outdir $folder
However, this command will cause the following message to appear:
./createpdf.sh: line 8: /opt/libreoffice3.4/program/soffice: Permission denied
The line itself is fine. If I echo it and run it manually in SSH, it works fine. This is most likely because I'm running it as a different user (note: not as the user that installed it, and not as root), with different permissions.
Desperate as I was, I've already tried chmodding the entire libreoffice folder to 777, and tried to make the 'apache' user the owner. No luck there. Would anyone have a clue as to why it's not letting apache run it, and how I can solve this?
As an alternative to running from the command line, have you considered running UNO (the Open/Libre Office alternative to COM) with the PUNO PHP wrapper. That way, you don't need to worry about permissions
See also Universal Network Objects (UNO): there are Python, Java, etc. bridges for use it. There are applications for simplify the use for convertions, see Docvert and JODConverter (jODconverter and pyODconverter).
All of then can be called as web-service or exec by PHP.
I work at a small computer shop, and we have to analyze windows minidumps all the time. My idea was to install the Windows Debugging Tools on a windows PC and use apache/PHP as an interface to it. That way I could just set up an HTML upload form that would accept the minidump file, run it through KD, then spit out the output.
It nearly works. I created a special user just for apache so I could assign it write privaleges to C:\symbols, and I use the following code:
<?php
$kdScript = "\"\\Program Files\\Debugging Tools for Windows (x86)\\kd.exe\" -c \"!analyze -v;Q\" -y srv*c:\symbols*http://msdl.microsoft.com/download/symbols -z ";
$kdScript .= $_FILES["myFile"]["tmp_name"];
$output = `$kdScript`;
print("<pre>$output</pre>");
?>
The problem I'm having is that the symbols are not downloaded as they should be. I've verified apache is running as the user I think it is by calling "whoami" from inside backticks. I've verified that I can run the windows version of wget from within backticks, so I have access to the network. I can file_put_contents() into a new file under C:\symbols, so I have file creation permissions.
Also, I tried having PHP simply output the command to the browser so I could copy and paste it into a terminal. I was able to run a command prompt as my apache user via "runas", paste the command from PHP's output into the prompt, and it worked as expected, downloading all the symbols it needed to C:\symbols. Of course, I had to point it to a dump file NOT in the PHP temp directory, but this shouldn't make a difference.
What could be the problem? Just as a side note, all of this is local on a trusted pc in a company that has a total of 3 employees/owners. Security for this project is irrelavent.
Not sure what your exact problem is, but the symbol server client code is finicky and not very debuggable, it took us lots of tinkering to implement our version of this. You can always direct folks there or use it yourself:
http://www.osronline.com/page.cfm?name=analyze
-scott
I'm attempting to get PHP to call a batch file which will take an RTF file and convert it to a PDF using an OpenOffice macro. I've tested the batch file on the command line and it works fine, but I'm not having any luck calling and using the same batch file from PHP.
My machine OS is XP professional SP 3. I'm running IIS 6 and PHP version 5.2.9.
I've granted execute permissions to the internet user on c:\windows\system32\cmd.exe.
I specified the full path to the batch file being executed and the full path to the RTF file to be converted.
The PHP looks like this where $arg is the RTF to be converted:
$arg = "C:\\web_root\\whatever\\tempOutput.rtf";
$command = "c:\\windows\\system32\\cmd.exe /c c:\\web_root\\whatever\\convert.bat $arg";
Then inside a try-catch I call the exec command:
exec("$command 2>&1 && exit", $ret, $err);
I echo the results after the catch:
echo "ret: ";
print_r ($ret);
print "<br>";
echo "err is ";
echo $err;
print "<br>";
echo "DONE!";
And this is what I see:
ret: Array ( )
err is 0
DONE!
The RTF file does not get converted and I'm not seeing the errors. Any ideas on what I can try next? Thanks!!!
I'm going to bet this is about permissions.
In a typical setup, PHP runs as apache - so you'll want to make sure apache has the rights to execute the batch file.
also, check this relevant SO question, and this google search.
Looks like the output array is empty. Is your batch script supposed to have output?
Also, escapeshellcmd and escapeshellarg should be used
Are you using IIS as your webserver? If so, the PHP exec function will not work by default and you should NOT circumvent the security measures that prevent it from running.
Check your event viewer and you should find some errors pertaining to your problem. Run a query through google for: IIS PHP exec. This should give you a large selection of information about the problem.
Basically, the PHP exec function tries to fork a new cmd.exe instance. IIS prohibits this because it could open a security hole in the system.
The best solution that I have come up with is to have your php script either write the command that you want to execute to a flat file or make a database entry. You will then need to write a seperate script that is launched by the windows scheduler to run every 10 minutes or so that will check your flat file or database for commands to run. The new script will then run the commands and then place either the results or an execution confirmation that your web app will be able to access at a later time.
It's a kludge for sure.
Is PHP running in safe-mode? If so, shell commands are escaped with escapeshellcmd. Perhaps this is the problem?
Do you have control of the server running the PHP script?