How to execute WKHTMLTOPDF from PHP? - php

I know this topis is there already, but none of the answers helped me... I copied wkhtml folder from my HDD onto the server. When I run
exec('"../wkhtmltopdf/wkhtmltopdf.exe" "www.estiroad.com/export.php" "C:/EXTREM.pdf"');
nothing happens... Do I type the paths correctly? I mean, I need to type exact path to the wkhtmltopdf according to where I run the exec command from, right? And if I want to save it onto users HDD, I need to use absolute path, right? Strange is, that it gives me no error, just silently does nothing... I found about PHP bindings, but I don´t understand how to use them... Everybody solves that problem only in Linux and thats worthless for me :( Any help will be appreciated.

You should't be putting the quotes around the library.You can catch the output of the command this way:
$commandString = '../wkhtmltopdf/wkhtmltopdf-i386 http://www.estiroad.com/export.php file.pdf 2>&1';
$output = shell_exec($commandString);
The 2>&1 in UNIX will mean that the output will come through. 1 is stdout. 2 is stderr.
Hope this helps.
Or in windows
$commandString = '../wkhtmltopdf/wkhtmltopdf.exe http://www.estiroad.com/export.php file.pdf 2> output';
print $out ? $out : join("", file("output"));
From the permission issue it looks like you're running the production script on linux. Go to your production server and run
$ uname -a
You'll get something like:
Linux ora100 2.6.5-7.252-smp #1 SMP Tue Feb 14 11:11:04 UTC 2006 x86_64 x86_64 x86_64 GNU/Linux
the x86_64 suggest you're running a 64 bit CPU, if that's the case download the amd64 version of binary, otherwise download the i386 one. Both can be obrained from this url: http://code.google.com/p/wkhtmltopdf/downloads/list
Keep the windows binary. Have you got a config file? if you do make sure you have a switch where you assign your library path to a constant based on your environment.
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// this is windows server
define('WKHTML_LIB', "../wkhtmltopdf/wkhtmltopdf.exe");
} else {
// or the 64 bit binary?
define('WKHTML_LIB', "../wkhtmltopdf/wkhtmltopdf-i386");
}
Then change your code that initiates wkhtmltopdf:
$commandString = WKHTML_LIB' http://www.estiroad.com/export.php file.pdf 2> output';
print $out ? $out : join("", file("output"));

Use snappy from knplabs. Its a wrapper around wkhtmltopdf and handles the cases.

(I have experience with wkhtmltopdf, but ONLY on *nix)
My advice: First do EVERYTHING without PHP, no exec().
Simply make sure you can type on the commandline the command to create the PDF.
Are you sure you installed WebKit on the machine? wkhtmltopdf.exe depends on it.
Only after you are sure you can generate from commandline, try to translate your action to PHP. ALso make sure PHP has appropriate rights to execute wkhtmltopdf.exe, AND has appropriate right to write away to C:/EXTREM.pdf.

Related

PHP exec not working for compiling on Windows 8 through Visual Studio 2012 Command Prompt

Thank you for taking the time to help me today. I have what I hope is a simple question. I have been attempting to use php exec() or any related PHP command to Open up the Developer Command Prompt for Visual Studio 2013 and use it to compile a file and save the output to a file on my local machine. I have it working fine from Run on Windows, but I can't seem to get it to work with PHP exec(). Here is how I have the command set up currently.
$cmd = 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat && cd C:\wamp\www\csc424Try3\app\uploads && cl /EHsc basic.cpp && basic >> C:\wamp\www\csc424Try3\app\outputs\output.txt';
exec($cmd, $result);
As you can see, I am chaining together commands. The first command allows the prompt to open, the second changes to the proper directory, the third runs the command for compiling in the prompt, and the fourth outputs to a text file.
Any ideas where I'm going wrong? I had a feeling it had something to do with formatting, but perhaps chaining the commands together does not work in PHP the way it does on the system.
You're forgetting quotes around most of your arguments in the exec call's string, meaning that things like Program Files will be seen as two separate things, not a single directory.
$cmd = '"C:\Program Files (x86)\....VsDevCmd.bat" && etc...';
^-- ^-- need these
as is, you're trying to execute a program called C:\Program, with some extra arguments like Files (x86)\......
I found the answer to my own question after some research. The way I was trying to do it before just wasn't working. After talking to a colleague, I thought writing a .bat file for the background would work better, and it absolutely did. I wrote the chain of commands in a .bat file and executed it like follows;
system("cmd /c C:\wamp\www\csc424Try3\app\uploads\stuff.bat");
Success! The file can now be successfully compiled and ran from PHP. :) It is a good day.

How to determine path to php.exe on Windows - search default paths

I'm currently developing a couple of plugins for Sublime Text 2 on OS X and I would like to make them cross platform, meaning I have to find out if and where php.exe is installed.
Right now I call /usr/bin/php in Python, which obviously works only on OS X and Linux:
phppath = '/usr/bin/php'<br>
pluginpath = sublime.packages_path() + '/HtmlTidy/tidy.php'<br>
retval = os.system( '%s "%s"' % ( phppath, scriptpath ) )
But on Windows, there seems to be no definitive default path for php.exe. The more I googled for it, the more possibilities showed up. So far I think I would have to check each of the following paths for existence:
c:\php\php.exe
c:\php5\php.exe
c:\windows\php.exe
c:\program files\php\php.exe
c:\wamp\bin\php\php5\php.exe
c:\xampp\php\php.exe
That's already quite a collection, but what I'm asking for is either a complete list covering all possibilities - or another way to figure it out that should be as robust as checking each possible path.
So if you have php.exe installed in some place other than these, please leave a comment with your path and I will add it to the list above.
Besides, there seems to be php.exe and php-cli.exe. I guess it would be OK to loop through each possible path. Check first for php-cli.exe, check for php.exe, and take the first match. Is that correct or is there a better practice?
If the user has added PHP's bin folder to the system PATH, then you should just be able to try and execute php -v to check that it's present.
If you want to obtain the full path to the PHP executable and the target system is Windows Server 2003 or later (so Windows Vista, and Windows 7) then you could use the WHERE command, i.e.:
C:\> where php.exe
C:\Program Files (x86)\WAMP\bin\php\php5.3.5\php.exe
Also see possibly related question: Is there an equivalent of 'which' on the Windows command line?.
If you are really desperate to find any file on the user's computer, you could try executing the equivalent of a find - but it's going to be slooow!
C: && cd \ && dir /s /b php.exe
On powershell or commad prompt
php -r "echo PHP_VERSION;" gives the version
See other PHP constantas:
https://www.php.net/manual/es/reserved.constants.php
to answer your question
php -r "echo PHP_BINARY;" gives the full path to php.exe
if needed, to remove the php.exe
php -r "echo str_replace('php.exe', '', PHP_BINARY);"
From PHP 5.4 and later, you can use the PHP_BINARY constant.
Use:
C:\xampp\php\php.exe
It should work if your XAMPP instance is on D: or E:. You change it accordingly from C:.
I found a way for you to find out where the php.exe is stored. In C:, click the search bar and search for "php.exe". Then, there will appear an application called php and click it. Then, cmd will open and in the cmd tab it will say php.exe at the end. That's where your php.exe is and you can put the link address of the file in VS Code.
Try
echo PHP_BINDIR;
It works. I tested it on macOS as with PHP 5.6.

PHP exec using LP command fails on Mac OS X

I am trying to spool a file to a printer attached to the server using PHP.
I send a command such as lp -d 'Brother_QL-570' '/Users/user_name/Documents/wwwroot/mmsprint/mmsUw8Vv9 using EXEC in PHP and get a return value of 5. The command works fine in the terminal window.
If I send ls, whoami etc, the command executes fine.
whoami and ls live in /bin, so I created a symbolic link for lp and that made no difference.
Also tried /usr/bin/lp and that did not work either. I'm pulling what little hair I have left out over this.
Safe mode is off. The path variables are the same between PHP and my terminal environment.
I am running MAMP on Lion.
FWIW, the command cat /Users/user_name/Documents/wwwroot/mmsprint/mmswKJqYK | lp -d Brother_QL-570 has a return value of 133.
This is my code:
$temp_file = tempnam("./", 'mms');
$pdf->Output($temp_file, "F");
$pdf->Close();
$cmd = "lp -d 'Brother_QL-570' '".$temp_file."'";
exec($cmd,$output,$retval);
error_log("-----cmd=".$cmd."\n", 3, "printCard.log");
foreach ($output as $a) {
error_log("-----output=".$a."\n", 3, "printCard.log");
}
error_log("-----retval=".$retval."\n", 3, "printCard.log");
Any thoughts? What am I missing? This should be easy. Argh!!
It turns out to be a problem with MAMP. I googled "MAMP exec fails" and came up with several good answers. Its a compatibility problem with libraries. I added DYLD_LIBRARY_PATH='' && to the command and Bob's my uncle. Thanks for the replies.
What is the output from the command?
Most likely the PHP user (apache probably) has no permission to print things. Add that user to the lp group. (Assuming macs work similar to unix in this.)
I found a similar solution to the one mentioned about using DYLD_LIBRARY_PATH='' &&. Using this worked for me, so I tried to find a more permanent fix than including this in all my code. I opened up the file /Applications/MAMP/Library/bin/envvars and commented the following two lines out:
DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
export DYLD_LIBRARY_PATH
After restarting apache, this is working GREAT! Not sure why this is happening to me though. Might have to do something with the fact that I am running 2 virtual hosts.

Execute php script (Windows)

A few days ago I published this thread, and it was solved succesfully on Unix.
Now I moved to Windows and I have the same problem.
I need to execute a command from the web application. It must execute a .jar stored in the system. I looked on Google and found some changes that I needed to do.
The function to execute the command is:
<?php
if($_POST["name"] == "")
echo "name is empty";
else{
$path = $_POST["name"];
//$command = 'DISPLAY=:0 java -jar '.$path'; -> Used in Unix
//$command = 'java -jar ../../../../simulaciones/tanqueCalentamiento.jar';
$command = 'java -jar ..\..\..\..\simulaciones\tanqueCalentamiento.jar';
//system($command);
exec($command);
}
?>
The comments are different options I have tried. $path is the argument, but decided to put directly the path, to clarify my question:
Do I need a kind of "trick" as I did for Unix?
The one I use:
function _exec($cmd)
{
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run($cmd, 0,false);
error_log($cmd);
return $oExec == 0 ? true : false;
}
/!\ If your application has an interface, and if you are using vista or 7, your server can't be run as a service!
You're just starting 'java'. That means that the Java executable (java.exe) must exist, and must be able to be executed. Since you don't specify a path, it must be in a directory that is in the %PATH% environment variable.
To test, simply start cmd and type java<enter>. If java gets executed, you'll know at least you'll have java installed and startable. If it doesn't, check if you have java and specify the correct path to the java executable.
You can also run the .jar from the command line to see if it works. If it works like this, it is likely to work in PHP too.
In windows vista, 7 (I don't know if in others too), there is a problemm with xampp.
The apache does not stop, doesn't matter what you do, even switch the computer off. At least for me, the solution was:
Uninstall xamp and reinistall it.
Start/stop it always as administrator:
path\xampp\xampp_start
path\xampp\xampp_stop
Right click: run as administrator.
In this way apache is finished properly, and I can run my command with no probleems anymore
I hope this helps
http://drupal.org/node/224342

Problems when trying to exectue exec("unix2dos xxx") in PHP/Apache

In a previous post, I was trying to update the encoding for a download file from php. One of the suggestions was to run the unix2dos command before sending the file to the user. This works great when I run the command on the linux box, but when I try and run the command from php I get nothing. Here is what I tried:
$cmd = "unix2dos -n $fullPath $downloadFile";
echo exec($cmd, $out, $retVal);
This displays nothing to the screen, $retVal is 0, and $out is an empty string.
echo system($cmd, $retVal);
This displays nothing to the screen, $retVal is 0.
echo shell_exec($cmd);
This displays nothing to the screen.
I have also tried escaping the command and it parameters like:
$cmd = escapeshellcmd($cmd);
and
$cmd = "unix2dos ". escapeshellarg("-n \"$fullPath\" \"$downloadFile\"");
Please let me know if you see something that I am doing wrong.
Thanks!
Edit: Here is some info that may be helpful.
unix2dos version: 2.2 (1995.03.31)
php version 5.2.9
Running in apache 2 on in Redhat Enterprise Linux 4
Have you considered a pure PHP solution?
<?php
$unixfile = file_get_content('/location/of/file/');
$dosfile= str_replace("\n", "\r\n", $unixfile );
file_put_contents('/location/of/file/', $dosfile);
?>
Something like that should do it, although untested :)
Shadi
See which user the PHP exec command is running as:
<?php system('whoami'); ?>
If this command fails then you likely do not have permission to use exec() or system(), so check your INI files. But be sure to check the correct ones! On Debian systems there are separate Apache and CLI INI files stored at /etc/php5/apache/php.ini and /etc/php5/cli/php.ini respectively. Sorry I do not know the locations for RedHat.
If the whoami command succeeds, make sure that the unix2dos command can be run by the user that is shown, and that the same user is allowed to make changes to the files in question by using chmod or chown.
Are you using the full path to unix2dos? Perhaps the executable is in your path for your shell but not in the path that PHP is using.
My implementation of unix2dos produces no output. If the return value is 0 then the command succeeded and your file has been updated.
The only other thing I see is the -n option which my version doesn't seem to have. You should probably check your man page to see what options it supports
unix2dos does not display the file it converts. Therefor you must display it yourself. A very basic way to do it could be :
$cmd = "unix2dos -n $fullPath $downloadFile";
echo exec($cmd, $out, $retVal);
include "$fullPath."/".$downloadFile;
Using include is pretty dirty but quick and easy. A cleaner way would be to use fopen and read the file then display it.
You'd better create a function that enclose all the operation : conversion + display so you'll have everything at hands.
But, If I were you, I'd prefer to not use exec at all and use FileIterator with a trim on every line so you will not have to care about the carriage return nor deal with a hazardous shell binding.
Not sure about your exact problem, but debugging suggestion:
Try first setting $cmd to ls. See if that works. Then try using /bin/ls (use the full path.)
If those don't work, then there might be a problem with your PHP configuration - there might be a safemode parameter or something which disallows the use of exec(), shell_exec(), or system() functions.
I got the source code from here.
http://www.sfr-fresh.com/linux/misc/unix2dos-2.2.src.tar.gz
I compiled it and then ran the tool. This was my output:
rascher#danish:~/unix2dos$ ./a.out -n 1.txt 2.txt
unix2dos: converting file 1.txt to file 2.txt in DOS format ...
I think the problem is this: the program writes all of its output to stderr, rather than stdout. If you look at the source code, you can see "fprintf(stderr, ...)"
As far as I know, PHP will only read the part of your program's output that is sent to STDOUT. So to overcome this, it seems like you have to redirect the output of your program (unix2dos uses stderr) to stdout. To do this, try something like:
$cmd = "unix2dos -n $fullPath $downloadFile 2>&1"
The "2>" means "redirect stderr" and "&1" means "to stdout".
In either case, I would imagine that the file was converting properly, but since you weren't getting any of the expected output, you thought it was failing. Before making the change, check on the output file to see if it is in DOS or UNIX format.

Categories