23

An error occurs when converting a file using Libreoffice on ubuntu:

CompletedProcess(args=['soffice', '--headless', '--convert-to', 'txt:Text', '/var/www/Project/temp/e4bac2c2e7c04eb79cfa522967a30dd3.docx', '--outdir', '/var/www/Project/temp/'], returncode=77, stdout=b'', stderr=b'javaldx failed!\nWarning: failed to read path from javaldx\n')

Using subprocess:

process = subprocess.run(['soffice', '--headless', '--convert-to', 'txt:Text', path_docx, '--outdir', settings.TEMP_ROOT], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout)

~# java --version

openjdk 11.0.6 2020-01-14 OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1) OpenJDK 64-Bit Server VM (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1, mixed mode, sharing)

Evgenij Tsvetkov
  • 331
  • 1
  • 2
  • 4
  • 3
    The problem is caused because your application runs in an environment without the proper rights. I run into the same issue, i can reproduce it outside of my java.exec call: `sudo -u tomcat8 soffice --headless.....` I think appArmor is preventing libreOffice from opening another executable which is needed for the transformation. Will keep trying solving this issue and update. – Patrick Werner May 29 '20 at 17:03

6 Answers6

21

Ok found the solution:
If you are using libreoffice in headless, with a non root user, trying to convert a docx to a pdf, getting this error:

javaldx failed!
Warning: failed to read path from javaldx

Your user doesn't has a home folder set, or the home folder is not writeable. I just switched from calling libreoffice directly to calling it via a shell script like this:

export HOME=/opt/fhir-services && /usr/lib/libreoffice/program/./soffice --headless --invisible --convert-to pdf --outdir /opt/fhir-services /opt/fhir-services/tmp.docx

tomcat8 is the owner of this folder, now the conversion works.

Patrick Werner
  • 1,106
  • 9
  • 24
  • 17
    For those, who only get the `Warning: failed to launch javaldx - java may not function correctly` without any further specification, this solution worked for me on Ubuntu 22.04: `sudo apt-get install libreoffice-java-common` – wolfmanx Aug 18 '22 at 19:46
  • 1
    @wolfmanx: Your solution deserves more than being just a comment. Why don't you post it as an answer? – Pierre François Oct 14 '22 at 06:41
  • 1
    Agreed! What a lifesaver, @wolfmanx ! – JoHKa Nov 02 '22 at 02:31
12

Expanding @wolfmanx's comment: The following worked for me on Ubuntu 18.04

sudo apt-get install libreoffice-java-common default-jre

Especially default-jre was necessary. Moreover: note, that this seems to be "just" a warning. The operation may work in spite of that.

tyrex
  • 8,208
  • 12
  • 43
  • 50
1

if using python and calling libreoffice through python subprocess, you need to specify HOME directory:

 subprocess.run(
    [
        <conversion command>
    ],
    env={"HOME": <any_path_where_permission_is_granted>},
)
Sheldon
  • 11
  • 1
0

Look under your home directory for a .config directory, look under there for a libreoffice directory, make sure you own it and everything under it:

chown -R yourlogin.yourgroup libreoffice

Worked for me.

0

To solve this problem on AWS Lambda, you can use this code:

import subprocess
import tempfile

temp_dir = tempfile.TemporaryDirectory()
temp_dir_path = temp_dir.name

subprocess.run(
    f"soffice --headless --convert-to pdf {temp_dir_path}/input.xlsx --outdir {temp_dir_path}",
    shell=True,
    check=True,
    # libreoffice needs to create a dir called .cache/dconf in the HOME dir.
    # So HOME  must be writable. But on aws lambda, the default HOME is read-only.
    env={"HOME": temp_dir_path},
)

It works because the only folder you can write to on a lambda is /tmp, and libreoffice needs to be able to create a dir in your HOME dir.

Lucidity
  • 1,299
  • 17
  • 19
0

I want to add a solution I found while experiencing this issue.

I am running Libreoffice 6.4, Java openjdk 1.8.0, Apache 2.4.37 and PHP 8.2 as the restricted "apache" user with no home directory on an Oracle Linux 8 server that has SELinux enabled and enforcing. I have a php script with an exec statement that launched "soffice" to convert a file to html, and it was supposed to display the preview on the webapp.

$sofficeCmd = "export HOME=/tmp && /usr/bin/soffice --headless --convert-to html:HTML:EmbedImages $uploadedFilePath --outdir $uploadDir";

exec($sofficeCmd, $return, $res);

And I was still getting the error:

javaldx failed! Warning: failed to read path from javaldx LibreOffice 6.4 - Fatal Error: The application cannot be started. Extension Manager: exception during enableExtension

I had been struggling because I had tried almost all of the different recommended solutions I'd found across the many other stackoverflow/superuser/etc forum posts about this issue and it made no difference in the error.

What ended up being the problem was SELinux. The moment I set SELinux to permissive, the webapp correctly displayed the preview. There were two things I did to fix the issue:

1: I created a $webroot/tmp folder to act as the $HOME, and I set the folders SELinux type to "httpd_sys_rw_content_t".

2: Due to LibreOffice requiring Java, I also had to set the "httpd_execmem" SELinux boolean.

Disclaimer, According to the RedHat Booleans documentation;

httpd_execmem

When enabled, this Boolean allows httpd to execute programs that require memory addresses that are both executable and writable. Enabling this Boolean is not recommended from a security standpoint as it reduces protection against buffer overflows, however certain modules and applications (such as Java and Mono applications) require this privilege.

I'm not 100% on the security implications here, but knowing soffice needs access to Java, and is being ran in a restricted environment through the apache user, this fixed the issue for me. Hopefully this solution is appropriate and someone else finds this useful :)