I'm trying to convert HTML file into RTF file using unoconv in php.
From php I'm calling :
......
file_put_contents("/tmp/unoconv55b2862fea753.html", $content);
$command = "unoconv -f rtf -o /tmp/unoconv55b2862fea753.rtf /tmp/unoconv55b2862fea753.html";
exec($command, $output);
$converted = file_get_contents("/tmp/unoconv55b2862fea753.rtf");
the problem is, that file_put content will save .html, but unoconv for some reason doesn't save converted file into /tmp directory.
When I run that $command directly on server in console, converted file was created.
Do you have any idea where could be problem?
The problem could lie with PHP not having the right privileges for the tmp directory.
Try saving the converted file to the root of your site (or a subfolder within it) and see if it converts successfully.
When configuring unoconv on ubuntu server to be run under apache (www-data) user
This solution is based on:
https://github.com/dagwieers/unoconv/issues/87#issuecomment-16563550
and
https://github.com/dagwieers/unoconv/issues/87#issuecomment-32084464
Step-by-step guide
Follow these steps:
1. Create new file which will contain sudoers entry for user www-data:
sudo vi /etc/sudoers.d/www-data
2. Add following content to newly created file and save:
www-data ALL=NOPASSWD: /usr/bin/unoconv
3. In PHP exec() use "sudo unoconv"
Related
I'm currently uploading a DOC or DOCX file via DropzoneJS and then convert it to PDF in the server using LibreOffice PHP exec. If I typed it manually in the server then it will convert but if I run it via browser then it is not converting. I'm trying to run the soffice without a sudo but the command run via browser is not working. How can I convert it via browser run?
Manual Command in Terminal:
/opt/libreoffice5.2/program/soffice --convert-to pdf
/var/www/html/my_system/public/msword.doc --outdir
/var/www/html/my_system/storage/app/quotations
Command ls -liah results for folders:
my_system, storage, app, quotations = drwxrwxrwx apache apache
My sudo visudo results (pasted lines with no comments in order):
Defaults requiretty
Defaults !visiblepw
Defaults always_set_home
www-data ALL=NOPASSWD:ALL
robert ALL=NOPASSWD:ALL
apache ALL=NOPASSWD:ALL
PHP File:
$command = '/opt/libreoffice5.2/program/soffice --convert-to pdf /var/www/html/my_system/public/msword.doc --outdir /var/www/html/my_system/storage/app/quotations';
if (!$return) {
echo "PDF Created Successfully";
} else {
echo 'PDF not created. Command = ' . $command . '=' . $return;
}
Thanks.
Found the correct answer via https://superuser.com/questions/627266/convert-file-to-pdf-using-libreoffice-under-user-apache-i-e-when-using-php
Quoting Correct Answer:
So, you need to i) give apache's user a home and ii) give it a
directory it has access to to write in. So, create a tmp directory in
the same folder where you store your webpage and then run the
following php code:
<?php
shell_exec('export HOME=/tmp && libreoffice --headless -convert-to pdf --outdir ./tmp /tmp/ayb/document_34.doc');
?>
I just tested and it works perfectly on my machine. Make sure your
./tmp has its permissions set to 777. Also, you may need to restart
apache if you play aroud with it too much. It stopped working for me
after a while when I made changes and I needed to restart it.
I have the following folder on my server, with the following files:
error-2015-12-20.log
error-2015-12-21.log
error-2015-12-22.log
And I written a PHP script that packs into .tar.gz with names earlier than the current date, moves the packed file into another partition, and removes the input files. It's done via exec().
The problem is that all of those operations require me to use sudo and provide password.
How can I deal with this?
Are you familiar with Linux file permissions?
If you want to execute a script you have to have the right to do so (by being in the group/owner with executable bit set), and if you want to read files you also have to have the right to read those files.
Otherwise you are forced to execute the file as sudo, because root can access all files.
Learn about this here: https://www.linux.com/learn/tutorials/309527-understanding-linux-file-permissions
As stated above it's a permission problem, to solve this let instruct sudo to let your php script run without prompting for a password:
Create a custom sudoers file :
sudo visudo -f /etc/sudoers.d/90_your_user
Copy the following contents in the newly created file (customizing the relevant section):
Cmnd_Alias PHP_SCRIPT = /path/to/my/script.php param1 param2
Cmnd_Alias ROOT_PROGS = PHP_SCRIPT
# Programs allowed to run without password prompt of your_user
your_user HOST=(root) NOPASSWD:ROOT_PROGS
You can then execute your program with sudo /path/to/my/myscript.php without permissions problem and without sudo prompts you for your password.
I have a bash script that takes a parameter is called in PHP by shell_exec(script.sh parameter). Basically, my goal is to call a script that is owned by another user that is not apache.
The script.sh script is a file that contains the following (right now there are some error handling commands):
#/bin/bash
whoami>>whoami
echo $1 >> parameter
while read f; do
env>>envoutput
sudo -i -u archivescriptowner /path/to/archivescript.sh -command archive >> output
done < $1
In my /etc/sudoers file , I have the following:
apache ALL=(archivescriptowner) NOPASSWD: /bin/bash -c /path/to/archivescript.sh *
When I run this script as by running su -s /bin/bash apache and pass a parameter, it works.
When I run it via my button in php, archivescript.sh does not execute
The whoami file has apache written to it
The parameter file has the right file written to it
env shows the following
Term=xterm
LD_LIBRARY_PATH=/path/to/library
PATH=/sbin/:usr/sbin:/bin:/usr/bin
PWD=/var/www/html
LANG=C
SHLVL=4
=/bin/env
PWD is outputting right, that is where my script is right now, it will be moved in the future.
The output file when it is ran by the button click is blank.
I am at a loss as to why this is not working. Any insight would be helpful. Please let me know if I need to give any additional information.
I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd('/path/to/archivescript.sh');
echo $return1; //return from your script
I am trying to convert .docx file to .html using php shell_exec in CentOS 6.5
My php code:
$command = "libreoffice --headless -convert-to html resume.docx 2>&1";
$result = shell_exec($command);
echo $result;
When I run the index.php at http://localhost/converter/ it gives me:
javaldx: Could not find a Java Runtime Environment! Warning: failed to read path from javaldx /usr/lib64/libreoffice/program/soffice.bin X11 error: Can't open display: Set DISPLAY environment variable, use -display option or check permissions of your X-Server (See "man X" resp. "man xhost" for details)`
while in terminal it is working perfectly:
cd /var/www/html/converter/
libreoffice --healdess -convert-to html resume.docx
here it creates resume.html in my /var/www/html/converter/.
Hi i have the same problem, i want to convert PDF's from DOCS created with PHP, i'm using OpenSuse 12.3 with LibreOffice, tried many things, finally i detect that the error is in folder:
1.- First check that you don't have disabled shell_exec in php.ini, and open_basedir don't restrict your access folders.
2.- Run the command as a simple user in shell (terminal)
export HOME=/tmp && soffice --headless --convert-to pdf --outdir /srv/www/htdocs/ /srv/www/htdocs/Creecimientos/sic/app/webroot/usuarios/2/8_Pagare_CreePersonas.docx
3.- If it works, you only have to put the correct folders in your code, when i run this code in PHP, it show me a blank page, so i check the access_log of apache for any hint:
[Java framework] Error in function createSettingsDocument (elements.cxx).
javaldx failed!
Warning: failed to read path from javaldx
terminate called after throwing an instance of 'com::sun::star::uno::RuntimeException'
Note: my error was in using export HOME=/tmp, i checked that the folder in root system has 777 for tmp, but the problem was that apache don't acces to it, maybe search for a relative folder of the script, but after test many things i only put a folder with permissons for wwwrun HOME=/srv/www/htdocs/folder_with_777
This is my final code, that works..
<?php
function word2pdf()
{
echo "Procesando";
$result = shell_exec('export HOME=/srv/www/htdocs/Creecimientos/sic/ && soffice --headless --convert-to pdf --outdir /srv/www/htdocs/Creecimientos/sic/ /srv/www/htdocs/Creecimientos/sic/app/webroot/usuarios/2/8_Pagare_CreePersonas.docx');
echo $result;
}
word2pdf();
?>
In fact, it prints: convert srv/www/htdocs/Creecimientos/sic/app/webroot/usuarios/2/8_Pagare_CreePersonas.docx -> /srv/www/htdocs/Creecimientos/sic//8_Pagare_CreePersonas.pdf using writer_pdf_Export, after succes.
I made other changes before in desesperate mode, but none of them solved the problem, tried to change owner to soffice wich found it witch $ ls -l $(which libreoffice), tried with 777, etc..
/* This command will work on centos 6 /7 with installation of libreoffice headless package */
First install package on centos as :
yum install libreoffice-headless
/* following code work to extract text format from */
<?php
$result = exec("export HOME=/tmp/ && /usr/bin/libreoffice --headless --convert-to txt:Text --outdir /tmp filePath");
var_dump($result);
?>
Most likely the user that LibreOffice is ran as, does not have a writeable home directory so LibreOffice fails to create it's config directory and then it cannot create it's config files and then fails to load Java, because it cannot write the default config. A bit silly I know.
Try adding this parameter: -env:UserInstallation=file:///tmp/whateverhere
I don't have enough reputation to comment on TD_Nijboer's answer, but the answer to his specific problem appears to be that soffice needs to be able to read & write config information somewhere. The first place it tries to do this is the libreoffice directory in ~/.config ('~' means "the current user's home directory").
In Debian, by default, the www-data user has the home directory /var/www, and does not have write permission there.
If you make sure it has permission to either create ~/.config itself, or libreoffice within an existing ~/.config, I expect it will work.
2 things, 1st the command is soffice --headless,
2nd i have an similar javaldx error and it has to do with permission.
when executing as root it works fine, but php executes as www-data.
if anybody knows a good way to execute libreoffice from php please let me know.
as i'm getting an error code 77 saying:
[Java framework] Error in function createSettingsDocument (elements.cxx).
javaldx failed!
Warning: failed to read path from javaldx
I'm new in using Linux, I'm trying to write a PHP code which can run .exe linux compatible file, I've made a short shell script
hello bash script:
#!/bin/bash
./program.exe file.mp4 // file.mp4 is an an input for .exe
echo "Hello World!"
shell.php:
<?php
$output = exec ("./hello ");
echo "<pre>$output</pre>";
?>
Now when I run shell.php using web browser it shows Hello World! but the .exe doesn't run, however when I run php using terminal command php shell.php, It works fine.
I think I'm having problems with permissions but I'm new with Linux and I don't know how to solve this.
Update:
I ignored the shell script and I used
<?php
$output = shell_exec ("cd /var/www/ && ./program.exe file.mp4 2>& " );
?>
also I granted access to program.exe
chmod 777 program.exe
the error I receive in the browser :could not open debug.bin!
use the absolute path to hello executable exec("sh path/to/the/file")
I'm using something similar to call an app compiled with mono on a remote ubuntu webserver and return it's output to the calling script.
For any of this to work properly wine needs to be already installed.
On Ubuntu systems try:
sudo apt-get -y install wine
You then need to know the owner of the web server process. If you are running the apache web server try the following:
cat /etc/apache2/envvars | grep "RUN"
The output will look something like this:
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
export APACHE_RUN_DIR=/var/run/apache2$SUFFIX
Now that you have the name of the process owner, which in this case is www-data you should ensure the file is owned the user and its group:
sudo chown www-data /var/www/program.exe
sudo chgrp www-data /var/www/program.exe
Finally, we can invoke the application from inside our PHP script by passsing it as a parameter to 'wine' and using its full file path.
<?php
$output = shell_exec("wine /var/www/program.exe file.mp4" );
?>
Any output from the above shell command sent to the command line will be saved in the PHP script variable $output.
It looks like you are trying to do some output redirection with your use of program.exe file.mp4 2>& so I've left that off of the example for clairity.
Try using the absolute path, such as exec("sh /path/to/file")
Generally, php is run as www or apache, so make sure that the execute access permission is granted to all user.