1

I'm having an issue trying to convert DOC/DOCX to PDF programmatically. I'm following this answer.

I'm using PyODConverter as mentioned in the answer above. I asked my web hosting to install OpenOffice and so they did, so when I ran whereis soffice it gave me /usr/bin/soffice.


So, I have this piece of code:

adocpdf

filename=$1
extension=$2
SERVICE='soffice'
if [ "`ps ax|grep -v grep|grep -c $SERVICE`" -lt 1 ]; then 
unset DISPLAY
/usr/bin/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard & 
sleep 5s
fi
python /home/[MYSITE]/public_html/docpdf/DocumentConverter.py /home/[MYSITE]/public_html/docpdf/$filename$extension /home/[MYSITE]/public_html/docpdf/$filename.pdf

index.php

<?php
    $filename = "teste";
    $extension = ".doc";
    $output = array();
    $return_var = 0;

    exec("/home/[MYSITE]/public_html/docpdf/adocpdf {$filename} {$extension}", $output, $return_var);
    print_r($output);
    print($return_var);
 ?>

My folder structure

enter image description here

After executing my php code, the output is the following, being Array() the $output and 1 the $return_var

Array ( ) 1

I don't know much about python, and I really need this to work!

Edit 1:

I ran `exec('pydoc modules') and it gave me this output:

Please wait a moment while I gather a list of all available modules...

ArrayPrinter audioop iniparse (package) select
BaseHTTPServer audit inspect selinux (package)
Bastion auparse itertools semanage
CDROM avc japanese (package) seobject
CGIHTTPServer base64 keyword sepolgen (package)
CORBA bdb kudzu sets
Canvas binascii lapack_lite sgmllib
ConfigParser binhex ldap (package) sha
Cookie bisect ldapurl shelve
DLFCN bonobo (package) ldif shlex
Dialog bsddb (package) libiscsi shm
DocXMLRPCServer bz2 libsvn (package) shutil
DocumentConverter cElementTree libuser signal
FFT (package) cPickle libxml2 site
FileDialog cStringIO libxml2mod smtpd
FixTk cairo (package) linecache smtplib
HTMLParser calendar linuxaudiodev snack
IN cgi locale sndhdr
LinearAlgebra cgitb logging (package) socket
M2Crypto (package) chunk macpath sos (package)
MA (package) cmath macurl2path sqlite (package)
MLab cmd mailbox sqlitecachec
Matrix code mailcap sre
MimeWriter codecs markupbase sre_compile
Numeric codeop marshal sre_constants
ORBit collections math sre_parse
PortableServer colorsys md5 stat
Precision commands mhlib statcache
Queue compileall mimetools statvfs
RNG (package) compiler (package) mimetypes string
RandomArray cookielib mimify stringold
ScrolledText copy mmap stringprep
SimpleDialog copy_reg modulefinder strop
SimpleHTTPServer cracklib multiarray struct
SimpleXMLRPCServer crypt multifile subprocess
SocketServer csv mutex sunau
StringIO curses (package) netrc sunaudio
TYPES datetime new svn (package)
Tix dbhash nis symbol
Tkconstants dbm nntplib symtable
Tkdnd dbus (package) ntpath sys
Tkinter decimal nturl2path syslog
UserArray difflib numeric_version tabnanny
UserDict dircache oddjob tarfile
UserList dis opcode telnetlib
UserString distutils (package) operator tempfile
_LWPCookieJar dl optik termios
_MozillaCookieJar doctest optparse test (package)
__builtin__ dogtail (package) os textwrap
__future__ drv_libxml2 os2emxpath this
_audit dsextras ossaudiodev thread
_bisect dsml pango threading
_bsddb dumbdbm pangocairo time
_codecs dummy_thread parser timeit
_codecs_cn dummy_threading pdb timing
_codecs_hk elementtree (package) pickle tkColorChooser
_codecs_iso2022 email (package) pickletools tkCommonDialog
_codecs_jp encodings (package) pipes tkFileDialog
_codecs_kr errno pkgutil tkFont
_codecs_tw exceptions platform tkMessageBox
_csv fcntl popen2 tkSimpleDialog
_curses filecmp poplib toaiff
_curses_panel fileinput posix token
_gamin fnmatch posixfile tokenize
_heapq formatter posixpath trace
_hotshot fpformat pprint traceback
_kudzu ftplib profile tty
_ldap gamin pstats turtle
_locale gc pty types
_multibytecodec gconf pwd tzparse
_numpy gdbm py_compile umath
_random getopt pyclbr unicodedata
_semanage getpass pydoc unittest
_snack gettext pyexpat urlgrabber (package)
_socket glob pygtk urllib
_sqlite gnome (package) pynche (package) urllib2
_sqlitecache gnomevfs (package) quopri urlparse
_sre gobject (package) random user
_ssl gopherlib ranlib uu
_strptime grp re warnings
_symtable gtk (package) readline wave
_testcapi gtkunixprint reconvert weakref
_threading_local gzip regex webbrowser
_tkinter heapq regex_syntax whichdb
_weakref hmac regsub whrandom
acutil hotshot (package) repr xdrlib
aifc htmlentitydefs resource xml (package)
anydbm htmllib rexec xmllib
array httplib rfc822 xmlrpclib
arrayfns idlelib (package) rgbimg xxsubtype
asynchat ihooks rhpl (package) yum (package)
asyncore imageop rlcompleter zipfile
atexit imaplib robotparser zipimport
atk imghdr rpm (package) zlib
atspi imp rpmUtils (package)
audiodev imputil sched
11
  • Have you tried running this on the shell yourself? Does soffice actually start? Also, from here: "Since it uses the Python/UNO bridge, the script requires the UNO modules to be already present in your Python installation. Most of the time this means you need to use the Python version installed with OpenOffice.org"
    – mabi
    Apr 5, 2013 at 9:53
  • @mabi is there a way to know if I have that module installed or check the python version on the webserver?
    – silentw
    Apr 5, 2013 at 10:00
  • with libreoffice you can cover all the files in the directory by using libreoffice --headless --invisible --convert-to pdf *.doc
    – root
    Apr 5, 2013 at 10:17
  • @root but isn't it going to run into the same problem, because (I think) there is no UNO module present? I kinda didn't want to ask my webhosting to install libreoffice now, since I asked to install OpenOffice like one day ago...
    – silentw
    Apr 5, 2013 at 10:50
  • Can you see the directory structure? Check if they installed a custom python version along with openoffice. If they did (check my first link for the location), just call it with the full path like suggested in the link instead of just "python".
    – mabi
    Apr 5, 2013 at 10:54

1 Answer 1

3

I have asked my Hosting Provider to install the latest LibreOffice and it just worked out ok!

I'm using the following code:

exec("/opt/libreoffice4.0/program/soffice.bin  --headless --convert-to pdf --outdir ".$path."  ".$filename.".".$extension, $output, $return_var);

I'm using Uploadify to upload the document to convert and then I'm giving the file back to the client to download.

Hope this question/answer helps anyone else in the world just like me, that had trouble with OpenOffice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.