14

I have installed libreoffice headless on a freebsd-server with apache in order to convert documents programmatically (e.g. odt->pdf). It works from the command line! But my goal is to be able to do it from php. This demands that the web-user (www) can run libreoffice. But it cannot.

When running libreoffice as my own user, I get:

%libreoffice --headless -convert-to pdf Litteraturundervisningogit.doc
javaPathHelper: not found #This should not be a problem, says people on the net.
convert /usr/home/bundsgaard.net/www/jeppe/foredrag/Litteraturundervisningogit.doc ->
 /usr/home/bundsgaard.net/www/jeppe/foredrag/Litteraturundervisningogit.pdf using writer_pdf_Export
%

If I try the same command as root, it does not work. The same is the problem with the www-user from php:

sp# libreoffice --headless -convert-to pdf Litteraturundervisningogit.doc
javaPathHelper: not found
sp#

The problem is that I do not get any information from libreoffice, thus I don't know why libreoffice does not want to run as other users than my own.

My question is: How do I give the www-user permission to run libreoffice via exec() in php?

TimWolla
  • 31,849
  • 8
  • 63
  • 96
user1176941
  • 141
  • 1
  • 1
  • 3

8 Answers8

17

I managed to fix this issue with a quick export HOME=/tmp before running the convert command, this gives libreoffice somewhere writable to work it's magic.

Question Mark
  • 3,557
  • 1
  • 25
  • 30
  • That is very smart... You probably saved me a lot of time. Thanks. – Scalpweb Jan 24 '16 at 01:34
  • This solves hanging conversions from PHP for me, but can you tell me what this exactly does? Why does this solve hanging conversions? – Rvanlaak Mar 11 '16 at 11:20
  • 1
    Libreoffice writes shadow/temporary/hidden files to the user's (the user running PHP, normally apache or http or www-data) home directory. The user running PHP from a webserver (not from CLI) never normally has a home directory, so the $HOME env variable will be blank or not set and libreoffice cannot write to /. So setting this explicitly to tmp gives libreoffice a bit of space with permission to write. HTH – Question Mark Mar 11 '16 at 12:45
3

Did you look at this blog post?

http://geekswithblogs.net/robertphyatt/archive/2011/11/19/converting-.docx-to-pdf-or-.doc-to-pdf-or-.doc.aspx

kguest
  • 3,804
  • 3
  • 29
  • 31
silverfighter
  • 6,762
  • 10
  • 46
  • 73
3

Not strictly an answer, but rather than using PHP's exec, you might consider using PUNO, a PHP5 module that provides access to the OpenOffice.org UNO Programming API.

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
2

This works for me.

Make sure you have installed java RE, for example in ubuntu:

apt-get install default-jre

First, find the location of your libreoffice

$ which libreoffice
/usr/bin/libreoffice

Include the folder location in the PATH, and also set the HOME var by adding these lines:

putenv('PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin');
putenv('HOME=/tmp'); 
system("libreoffice .....
Arief Karfianto
  • 235
  • 2
  • 8
1

I'll recomend put config path first you run exec() or shell_exec();

IE:

// Vars
putenv('PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin');
putenv('HOME=' . $outputdir); 

$outputdir = chmod 777 And the same forlder from libreoffice command "--outdir"

Adonias Vasquez
  • 1,014
  • 13
  • 11
1

It is working for me.

You can convert docx to HTML by using libreoffice, You need to give proper permission output directory

exec("export HOME='/var/www/html/wp/wp-content/uploads/' && /usr/bin/libreoffice --headless   --convert-to html --outdir '/var/www/html/wp/wp-content/uploads/' /var/www/html/wp/wp-content/uploads/demo.docx");
Ramesh
  • 1,495
  • 12
  • 14
1

I was having the same problem and yes (thanks Wrikken) after prepending /usr/local/sbin to the PATH environment variable I was able to run libreoffice as www-data under apache.

nickyspag
  • 115
  • 1
  • 7
0

About Universal Network Objects (UNO), there are some "plug-and-play" final-user tools, see Docvert and JODConverter (jODconverter and pyODconverter). All of then can be called as web-service or exec by PHP.

Peter Krauss
  • 13,174
  • 24
  • 167
  • 304