wkhtmltopdf not working in php script - php

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

Related

I can't find the format file `pdflatex.fmt'!

I want to run pdflatex from php on user request.
Running from terminal works fine [php file.php] and creates pdf file. But when I try to run from browser by sending request to php, It gives this error "I can't find the format file `pdflatex.fmt'!"
I am working with php5.2.6, OS is Fedora 9, and safe_mode is turned off.
Code -
$cmd = "/usr/bin/pdflatex /home/deep/public_html/resume/Folder/deependra.tex 2>&1";
$output = exec($cmd);
echo $output;
the pdflatex.fmt must be readable by the web browser in order to be accessible for the exec command, also the server might be chrooted and if so, the chroot folder might not have access to all the pdflatex parts. check it...

Cannot execute command using exec() in php

Ive tried searching for the answer but cant find anything proper in relation to my case.
What i want to execute is very simple...its a call to the SPMF framework jar file.
here is the code -
<?php
exec('java -jar spmf.jar run Apriori /opt/lampp/htdocs/rrugd/ip.txt /opt/lampp/htdocs/rrugd/output.txt %20');
?>
why is this not executing?
it obviously runs perfectly using cli.
the folder having all the files (jar file,ip.txt,output.txt) have permissions set to r/w to all users
I am on Linux - Ubuntu 13.10
I found the solution.
The problem was in the path to the spmf zip file.
Either it needs a full path or should be in the same folder as the script.
I solved it by placing it in the folder same as the script.( under htdocs as i am using lampp)
Also, the folder needs to have permissions for other to be set to "Create and Delete" for php to be able to access and write output to the folder.
Basic debugging: If exec() doesn't work, then you need to check return values:
exec('some command here', $output, $return_var);
var_dump($output, $return_var);
NEVER assume that an external resource succeeded. ALWAYS check return values and output for error messages.
Most likely java isn't in the path of whatever shell PHP is using to handle the exec() call, meaning you'll need exec('/full/path/to/java ...') instead.

PHP exec command works in shell and php command line but not on website

I have a problem where I am trying to copy a file that is generated on a server drive that I have mounted using the php exec command. However, the command doesn't work (although the return status is 1) when called from a web-page.
$src = "/mnt/...";
$dest = "/var/www/...";
exec("cp $src $dest");
I have tried printing out the command to make sure it is correct, and it is. I have also tried making sure the file exists before attempting to copy it and it is.
if (file_exists($src)) {
exec("cp $src $dest");
}
Copying the command directly into the terminal works.
$ >cp /mnt/... /var/www/...
I've also tried using the php command line tool to run the exec command and that also works.
$ >php -r 'exec("cp /mnt/... /var/www/...");'
I have also tried using shell_exec as well, with the same results.
Hostings usually disable the shell commands.
On your local, you can edit your php.ini and add this line to disable the functions you want:
disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
But on your live website, you need to contact your host provider to enable the functions that are disabled by default.
You can add the second parameter to help debug, $output will display what the cp command is doing, whether it be an error or not.
I would also recommend placing quotes around the files, just in case something with a space gets in there.
$src = "/mnt/...";
$dest = "/var/www/...";
exec("cp '$src' '$dest'", $output, $retrun_var);
var_dump($output, $retrun_var);
I had a similar issue a while ago. It was essentially what all the commentators are saying. The user/permission of the web server are restricted, but also the shell environment that is being used and/or the PATH environment variable.

PHP exec() with CUDA

exec("fun.exe input/input.txt ");
I want to run an CUDA program in PHP,
the task is:
load data from an input.txt. (argument)
calculating.
write an output.txt.
and PHP read ouput.txt to do next task.
In server1(Apache ,Windows XP), it can run perfectly,
but in server2,3(Apache, Windows 7),the output is wrong.
The program doesn't crash and there's no any error message in the page,
it seems like something wrong during the execution.
Next I try exec the All CPU-side version (same calculation),server2,3 can run correctly.
If I exec the fun.exe(CUDA version) in server2,3 directly(double click or in command line),the program also run perfectly.
Any idea on why server2,3 can't run the program? Thanks.
First, try using the full path to the executable. Then the full path to the input file too.
If that doesn't work, then try modifying the file permissions (try with full 777 permissions, if that works then you know where your problem lies).
Try to use the entire path (windows version using backslash).

Can't get shell_exec to download a file

I have a multithreaded cli downloader for ubuntu called Aria2c.
I've been trying to get a php script to run aria2c and download a file using shell_exec, but i can't seem to get it to work. Ultimately I plan to have an input box on a page where I can enter a link and aria would download it.
Here's the code I've come up with (for now im inputting the link manually):
<?php $dl = shell_exec('aria2c -d /home/user/ www.downloadlink.com'); ?>
Note that the aria2c command I specified works well in the shell; and the directory I'm attempting to download to is set to '777'.
I'm baffled as to why it's not working, any ideas?
PS: I prefer to use aria rather than the alternatives because it is multithreaded and it supports cookies.
Check if PHP is running in safe_mode. shell_exec won't work if safe_mode is on.
EDIT: aria2c was not referenced with a full path. Referencing it like this: shell_exec('/path/to/aria2c -d /home/user/ www.downloadlink.com') works.
I'll make the assumption that you are running PHP through a web server. In such case, it's very unlikely that the web server has permission to write into your user's home directory: Apache runs as daemon with the credentials of a limited user. Also, the PATH env variable in Apache is not necessarily the same as your user's PATH. Last but not least, you don't check the return value or the script output.
Try something like this:
<?php
exec('/path/to/aria2c -d /tmp www.downloadlink.com', $output, $return_var);
var_dump($output, $return_var);
?>
You can get the full path for aria2c with:
which aria2c

Categories