Merge pull request #27700 from mdeweerd/tools/odt2pdf

Qual: Improve odt2pdf, add pandoc
This commit is contained in:
Laurent Destailleur 2024-01-21 23:44:57 +01:00 committed by GitHub
commit a9db66d790
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,6 +3,7 @@
# @copyright GPL License 2013 - Florian HEnry - florian.henry@open-concept.pro
# @copyright GPL License 2017 - Laurent Destailleur - eldy@users.sourceforge.net
# @copyright GPL License 2019 - Camille Lafitte - cam.lafit@azerttyu.net
# Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
#
# Convert an ODT into a PDF using "native" or "jodconverter" or "pyodconverter" or "unoconv" tool.
# Dolibarr variable MAIN_ODT_AS_PDF must be defined ...
@ -18,41 +19,49 @@
# If you got this error: javaldx failed! Warning: failed to read path from javaldx with no return to prompt when running soffice --headless -env:UserInstallation=file:"/tmp" --convert-to pdf --outdir xxx ./yyy.odt,
# check that directory defined into env:UserInstallation parameters exists and is writeable.
# shellcheck disable=2046,2086,2268
if [ "x$1" == "x" ]
if [ "$1" = "" ] || [ "$2" = "" ]
then
echo "Usage: odt2pdf.sh fullfilename [native|unoconv|jodconverter|pyodconverter|pathtojodconverterjar]"
echo "Usage: odt2pdf.sh fullfilename [native|unoconv|jodconverter|pyodconverter|pathtojodconverterjar|pandoc]"
echo "Example: odt2pdf.sh myfile unoconv"
echo "Example: odt2pdf.sh myfile ~/jodconverter/jodconverter-cli-2.2.2.jar"
exit
fi
FILENAME=$1
METHOD=$2
# Full patch where soffice is installed
soffice="/usr/bin/soffice"
soffice=${SOFFICE:=/usr/bin/soffice}
if [ ! -x "$soffice" ] ; then
soffice=$(which soffice 2>/dev/null)
fi
if [ ! -x "$soffice" ] ; then
echo "Need path to soffice - install 'soffice' or set SOFFICE to path"
exit 1
fi
soffice_basename=$(basename "$soffice")
# Temporary directory (web user must have permission to read/write). You can set here path to your DOL_DATA_ROOT/admin/temp directory for example.
home_java="/tmp"
home_java=${HOME_JAVA:=/tmp}
# Main program
if [ -f "$1.odt" ]
if [ -f "${FILENAME}.odt" ]
then
if [ "x$2" == "xnative" ]
if [ "${METHOD}" == "native" ]
then
$soffice --headless -env:UserInstallation=file:///$home_java/ --convert-to pdf:writer_pdf_Export --outdir $(dirname $1) "$1.odt"
"$soffice" --headless "-env:UserInstallation=file:///$home_java/" --convert-to pdf:writer_pdf_Export --outdir "$(dirname "${FILENAME}")" "${FILENAME}.odt"
exit 0
fi
if [ "x$2" == "xunoconv" ]
if [ "${METHOD}" == "unoconv" ]
then
# See issue https://github.com/dagwieers/unoconv/issues/87
/usr/bin/unoconv -vvv "$1.odt"
/usr/bin/unoconv -vvv "${FILENAME}.odt"
retcode=$?
if [ $retcode -ne 0 ]
then
@ -62,11 +71,25 @@ then
exit 0
fi
nbprocess=$(pgrep -c soffice)
if [ $nbprocess -ne 1 ] # If there is some soffice process running
if [ "${METHOD}" == "pandoc" ]
then
cmd="$soffice --invisible --accept=socket,host=127.0.0.1,port=8100;urp; --nofirststartwizard --headless -env:UserInstallation=file:///$home_java/"
export HOME=$home_java && cd $home_java && $cmd&
pandoc "${FILENAME}.odt" -t pdf -o "${FILENAME}.pdf"
retcode=$?
if [ $retcode -ne 0 ]
then
echo "Error while converting odt to pdf: $retcode"
exit 1
fi
exit 0
fi
nbprocess=$(pgrep -c "$soffice_basename")
if [ "$nbprocess" -ne 1 ] # If there is some soffice process running
then
# shellcheck disable=2089
cmd="\"$soffice\" --invisible --accept=socket,host=127.0.0.1,port=8100;urp; --nofirststartwizard --headless \"-env:UserInstallation=file:///$home_java/\""
# shellcheck disable=2090
export HOME="$home_java" && cd "$home_java" && eval "$cmd" &
retcode=$?
if [ $retcode -ne 0 ]
then
@ -76,15 +99,15 @@ then
sleep 2
fi
if [ "x$2" == "xjodconverter" ]
if [ "${METHOD}" == "jodconverter" ]
then
jodconverter "$1.odt" "$1.pdf"
jodconverter "${FILENAME}.odt" "${FILENAME}.pdf"
else
if [ "x$2" == "xpyodconverter" ]
if [ "${METHOD}" == "pyodconverter" ]
then
python DocumentConverter.py "$1.odt" "$1.pdf"
python DocumentConverter.py "${FILENAME}.odt" "${FILENAME}.pdf"
else
java -jar $2 "$1.odt" "$1.pdf"
java -jar "${METHOD}" "${FILENAME}.odt" "${FILENAME}.pdf"
fi
fi
@ -97,6 +120,6 @@ then
sleep 1
else
echo "Error: Odt file $1.odt does not exist"
echo "Error: Odt file ${FILENAME}.odt does not exist"
exit 1
fi