LibreOffice

Installation Headless (Server-Betrieb)

Mit LibreOffice-Core landet auch das Java OpenJDK auf der Maschine (Stand 2022-12: immer noch OpenJDK 8).

RHEL 8+
# without specifying "libreoffice-writer" text documents can't be converted to PDF, for example
dnf -y install libreoffice-core libreoffice-writer libreoffice-calc libreoffice-impress libreoffice-draw libreoffice-math
RHEL 7
# without packet "libreoffice" no document conversion is possible
yum -y install libreoffice-core libreoffice
/usr/bin/soffice --headless --convert-to pdf myfile
/usr/bin/soffice --headless --convert-to pdf --outdir /tmp/ myfile

LibreOffice Headless mit Apache

Falls der Apache-Webserver LibreOffice beispielsweise per PHP-Skript ausführen können soll:

# configure libreoffice to run from inside of apache
mkdir -p /usr/share/httpd/.config/libreoffice
mkdir -p /usr/share/httpd/.cache
chown -R apache:apache /usr/share/httpd/.config
chown -R apache:apache /usr/share/httpd/.cache

Danach einmalig dem Benutzer Apache die Shell „/bin/bash“ geben und LibreOffice über das Shell-Skript ``soffice laufen lassen (das Shell-Skript setzt passende Umgebungsvariablen):

su - apache
/usr/bin/soffice --headless --convert-to pdf myfile

Benutzer Apache die Shell wegnehmen.

Damit wurde im Home-Verzeichnis des Benutzers apache ein Unterverzeichnis namens .config/libreoffice inklusive passender Cache-/Konfigurationsdateien erzeugt.

SELinux:

setsebool -P httpd_execmem on
setsebool -P httpd_can_network_connect on

semanage fcontext --add --type httpd_sys_rw_content_t '/usr/share/httpd/\.cache(/.*)?'
restorecon -Fvr /usr/share/httpd/.cache

semanage fcontext --add --type httpd_sys_rw_content_t '/usr/share/httpd/\.config(/.*)?'
restorecon -Fvr /usr/share/httpd/.config
Allow soffice.bin (via httpd) setattr access on the directory cache
cat >/tmp/my-sofficebin.te <<EOF
module my-sofficebin 1.0;

require {
    type lib_t;
    type httpd_t;
    type usr_t;
    class file write;
    class dir setattr;
}

#============= httpd_t ==============
allow httpd_t lib_t:dir setattr;

#!!!! WARNING: 'usr_t' is a base type.
allow httpd_t usr_t:file write;
EOF
cd /tmp
checkmodule --mls -m --output my-sofficebin.mod my-sofficebin.te
semodule_package --outfile my-sofficebin.pp --module my-sofficebin.mod
semodule --install my-sofficebin.pp
Allow jre/bin/java (via httpd) read access on sys/fs/cgroup
cat >/tmp/my-java.te <<EOF
module my-java 1.0;

require {
    type cgroup_t;
    type httpd_t;
    class file read;
}

#============= httpd_t ==============
allow httpd_t cgroup_t:file read;
EOF
cd /tmp
checkmodule --mls -m --output my-java.mod my-java.te
semodule_package --outfile my-java.pp --module my-java.mod
semodule --install my-java.pp

Ein passendes PHP-Skript, was zum Test im Kontext des Apache Webservers auszuführen ist:

<?php

print(filesize('myfile.docx'));
shell_exec('/usr/bin/libreoffice --headless --convert-to pdf --outdir /tmp/ myfile.docx');
// file is created at /tmp/systemd-private-cbae11a1dee34e959b880249be1ce7f9-httpd.service-cBqR1U/tmp/myfile.pdf
print(filesize('/tmp/myfile.pdf'));

Troubleshooting

javaldx failed! Warning: failed to read path from javaldx. LibreOffice 6.4 - Fatal Error: The application cannot be started. User installation could not be completed.

Das Verzeichnis $HOME/.config/libreoffice gehört nicht dem Benutzer, unter dem LibreOffice laufen soll.

Built on 2024-04-18