Run pdftk by php on linux centos - php

I would like to run pdftk on my webserver. It's a Linux Centos with PHP 5.3.2.
When I connect it by commande line I do
pdftk --version
It's OK
pdftk A=p1-9.pdf cat A1 output p1.pdf
It's OK.
Now, I do this by php :
exec(pdftk A=p1-9.pdf cat A1 output p1.pdf)
It isn't OK. Why?? I search about the link of file, but it looks OK.
This doesn't work too :
exec(pdftk --version)
I install pdftk with this How do I install Pdftk on my server?
So what's wrong??
Thank for your help!

I've run into this issue before. Assuming that you're wrapping your commands string in quotes (as gioele noted), the issue may be that you need to set your path when running the system command. Try this:
$command = "pdftk A=p1-9.pdf cat A1 output p1.pdf";
system("PATH=\$PATH:/usr/bin/ && $command",$response);
if ($response===FALSE){
//there was an error, handle it
}
(I've added a little response handling there as well). If that doesn't work, check to see what path you should use (it will depend on where you installed PDFTK).
I believe you can also get the same result by using putenv("PATH=" .[your path]); and I've used system() here, but exec() should be affected in the same way

Related

Where is the PHP path in CentOS 6.5 droplet?

Unfortunately I do not have access to the droplet via console, only by Vesta panel and I need to set up cron job.
However, I can not find the right path to PHP. Now that command line looks that and it does not work:
/usr/local/vesta/bin/php5 -c /home/admin/web/ -q /home/admin/web/mysite.com/public_html/crop_scripts/cron.php
Please tell me what options instead of "/usr/local/vesta/bin/php5" I can try?
what does this code show when you access it through your web server (assuming exec call is allowed)
<?php
$output = array();
exec('which php',$output);
var_dump($output);

Not able to run wkhtmltopdf commad through shell_exec() function in php but same command works on command line

I'm in trouble and that much confused about a php shell_exec command.
When the command is execute by PHP I have no error but the execution fails. If I use exactly the same command from a terminal it works.
Here's the command :
/usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyxa9otq.html" "/tmp/knplabs_snappyv3pD7h.pdf"
When I lauch this from a terminal :
$ /usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyWG9XTd.html" "/tmp/knplabs_snappyv3pD7h.pdf"
Loading page (1/2)
Printing pages (2/2)
Done
But from my php script :
// Construct the previous command
$command = $this->buildCommand($url, $path);
../..
shell_exec($command);
../..
$content = file_get_contents($path);
../..
I've test the output of shell_exec, it's empty.
The log :
Warning: file_get_contents(/tmp/knplabs_snappyv3pD7h.pdf): failed to open stream: No such file or directory in /*****/lib/snappy/SnappyMedia.class.php on line 64
No permission pb in the /tmp directory :
$ ls -la /tmp
total 448
drwxrwxrwt 16 root root 4096 mars 12 21:51 .
../..
I've tried avec the PHP exec() function to get error informations, I just get an "1" error code in return_var and nothing in output.
For information this issue appear on my test server, my desktop computer but not on my notebook. All the 3 are with sames PHP, Apache, Mysql versions.
I don't understand anything ...
Thanks for any help, I'm loosing my mind.
David.
I've found the solution here : Executing wkhtmltopdf from PHP fails
Thanks to Krzychu.
First to get information from the shell_exec command add " 2>&1" at the end of the command. In that way you will get information in return of the command :
$no_output = shell_exec($command);
echo $no_output; // nothing
$output = shell_exec($command . ' 2>&1');
echo $output; // in my case : "cannot connect to X server"
The solution :
Not use the wkhtmltopdf ubuntu package (0.9.9-4)
Use the official package from the Wkhtmltopdf download page
So no need to install xvfb ! (I've seen this advice many times)
Looks like a user's permissions issue.
When you run the command from the terminal, it is the user account, currently used, which does have the right permissions, to run a command in /usr/bin, and execute the specific file.
When you run it from the php script, it is the http server account on your system, which needs the permission to execute the file in /usr/bin. Usually this is the apache user.
How you should setup permissions depends on your system. Just remember that what is allowed for apache, is allowed for anyone accessing your http server.
I have had this problem for ages and adding . ' 2>&1' after the $command has somehow solved the problem.
this:
$output = shell_exec($command . ' 2>&1');
instead of:
$output = shell_exec($command);
No idea why but it works and I'm grateful.
Is it a shared hosting? It seems like shell_exec is a restricted function. Try running error_reporting(E_ALL); ini_set('display_errors', 1); before calling shell_exec.
I stumbled upon the same Problem, in my case an absolut Path in the exec Command like /var/www did not work, I had to use relative Paths from the point where I executed the php File.
I also wanted to notice, that it did not work using shell_exec, however it worked using normal exec command, not sure wheres the difference here.

how to run cmd in php exec function

I am trying to run a command on windows which is the follwing :
sfm+pmvs C:\\xampp\\htdocs\\temp\\$filename/ hello.nvm
I have done this but it wont work for me :
exec("C:\\xampp\\htdocs\\temp\\draw\\VisualSFM_win32.exe sfm+pmvs C:\\xampp\\htdocs\\temp\\$filename/ hello.nvm");
when I do the following :
exec("C:\\xampp\\htdocs\\temp\\draw\\VisualSFM_win32.exe");
it works just fine but I need the other paramters to be included in the command line
how could that be done in php and is there any way to open a cmd.exe and to run the command above ?
thanks in advance
Exec() just running in cmd command which you wrote as first argument of exec(). If VisualSFM_win32.exe not support arguments by run via cmd, you cannot run it as you want. Sorry for my english =)
You asked me for exapmle. My example below.
I am not sure it is 100% right because i don't use windows last 3 years.
#echo off
cd "C:\xampp\htdocs\temp\draw\"
start VisualSFM_win32.exe sfm+pmvs C:\xampp\htdocs\temp\%1/ hello.nvm
and in php
exec("C:\\test.bat " . $filename);

Uglify-JS won't run using shell_exec()

My question is similar to this:
Nodejs - .node_libraries/ for www-data
I've been trying all night to get php to execute uglifyjs. I've tried:
die(shell_exec('NODE_PATH="/home/app/nodejs/node_modules/"; export NODE_PATH; uglifyjs -o /home/app/public_html/js/profile.min5.js /home/app/public_html/js/profile.js'));
and
die(shell_exec('node /home/app/nodejs/node_modules/uglify-js/bin/uglifyjs -o /home/app/public_html/js/profile.min33.js /home/app/public_html/js/profile.js'));
and a bunch of other crazy combinations but nothing works. Everything works fine if I do it manually in the shell. Can anyone help please?
Have you tried looking at the following (I'll go for the obvious since you haven't said what you've already tried):
user apache/www-data is allowed access to /home/app : sometimes /home directories are closed off (chmod 0700)
Is safe_mode on your server in effect? From the command line, try:
php -i | grep safe_mode
Then, look at the output of:
phpinfo();
Which version of PHP are you using (might well be irrelevant)?

What is the difference between running a script from the command line and from exec() with PHP?

I'm trying to run a Python script using exec() from within PHP. My command works fine when I run it directly using a cmd window, but it produces an error when I run it from exec() in PHP.
My Python script uses NTLK to find proper nouns. Example command:
"C:\Python25\python.exe" "C:\wamp\projects\python\trunk\tests\find_proper_nouns.py" "I went to London this morning"
returns [London] when I run it from cmd, but throws an error in the Apache log when I run the same command from exec().The script is defintely getting run OK - if I change the python script to be print "Hello World" that is returned fine.
I know it's a big ask for anyone to know how to fix this NLTK error, but I could really do with any pointers as to why running it from exec is different to cmd. (The command is identical).
I'm running WAMP on Windows 7 with Apache 2.2.11.
Here's the error in the Apache log:
Traceback (most recent call last):
File "C:\wamp\projects\python\trunk\tests\find_proper_nouns_command_line.py", line 6, in <module>
parts = nltk.pos_tag(text)
File "C:\Python25\lib\site-packages\nltk\tag\__init__.py", line 62, in pos_tag
tagger = nltk.data.load(_POS_TAGGER)
File "C:\Python25\lib\site-packages\nltk\data.py", line 590, in load
resource_val = pickle.load(_open(resource_url))
File "C:\Python25\lib\site-packages\nltk\data.py", line 669, in _open
return find(path).open()
File "C:\Python25\lib\site-packages\nltk\data.py", line 451, in find
raise LookupError(resource_not_found)
LookupError:
**********************************************************************
Resource 'taggers/maxent_treebank_pos_tagger/english.pickle' not
found. Please use the NLTK Downloader to obtain the resource:
>>> nltk.download().
Searched in:
- 'C:\\nltk_data'
- 'D:\\nltk_data'
- 'E:\\nltk_data'
- 'C:\\Python25\\nltk_data'
- 'C:\\Python25\\lib\\nltk_data'
- 'C:\\Windows\\system32\\config\\systemprofile\\AppData\\Roaming\\nltk_data'
**********************************************************************
You have to run nltk.download() and choose 'maxent_treebank_pos_tagger'. You must make a python script and in it put:
#!/usr/bin/python
import nltk
nltk.download('maxent_treebank_pos_tagger');
then run it from command line. It will install the data files for the POS tagges, which you don't have installed yet.
After you do this it should work.
Your web server likely runs with other privileges than yourself. Possible problems include:
Path/file permission: can the web server user access the files it needs?
Different environment: are all necessary environment variables (PATH, Python-specific stuff, …) set?
Configuration: are there per-user configurations for Python or the module?
Tip: execute set in both the command prompt and from the PHP process and check the differences.
From the shell/terminal, you can use:
sudo python -m nltk.downloader maxent_treebank_pos_tagger
It will install maxent_treebank_pos_tagger (i.e. the standard treebank POS tagger in NLTK).

Categories