2013-04-07 17:27:35 +02:00
#!/bin/bash
2015-02-22 16:01:01 +01:00
# @copyright GPL License 2010 - Vikas Mahajan - http://vikasmahajan.wordpress.com
# @copyright GPL License 2013 - Florian HEnry - florian.henry@open-concept.pro
2017-03-14 10:51:28 +01:00
# @copyright GPL License 2017 - Laurent Destailleur - eldy@users.sourceforge.net
2024-01-10 19:47:29 +01:00
# @copyright GPL License 2019 - Camille Lafitte - cam.lafit@azerttyu.net
2024-01-21 02:26:08 +01:00
# Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
2013-04-22 15:50:26 +02:00
#
2019-12-30 17:25:54 +01:00
# Convert an ODT into a PDF using "native" or "jodconverter" or "pyodconverter" or "unoconv" tool.
2021-11-22 11:19:49 +01:00
# Dolibarr variable MAIN_ODT_AS_PDF must be defined ...
2024-01-10 19:47:29 +01:00
# to value "libreoffice" to call soffice native exporter feature (in such a case, this script is useless)
2019-12-30 17:25:54 +01:00
# or value "unoconv" to call unoconv CLI tool after ODT generation.
2017-03-14 10:51:28 +01:00
# or value "pyodconverter" to call DocumentConverter.py after ODT generation.
2017-10-10 18:54:30 +02:00
# or value "jodconverter" to call jodconverter wrapper after ODT generation
2017-03-14 10:51:28 +01:00
# or value "/pathto/jodconverter-cli-file.jar" to call jodconverter java tool without wrapper after ODT generation.
2015-01-18 14:57:25 +01:00
# Dolibarr variable MAIN_DOL_SCRIPTS_ROOT must be defined to path of script directories (otherwise dolibarr will try to guess).
2021-11-22 11:19:49 +01:00
#
2024-01-10 19:47:29 +01:00
# NOTE: Using this script is deprecated, you can now convert generated ODT to PDF on the fly by setting the value MAIN_ODT_AS_PDF
2021-11-22 11:19:49 +01:00
# to 'libreoffice'. It requires only soffice (OpenOffice or LibreOffice) installed on server (use apt install soffice libreoffice-common libreoffice-writer).
2024-01-10 19:47:29 +01:00
# 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,
2021-11-22 11:19:49 +01:00
# check that directory defined into env:UserInstallation parameters exists and is writeable.
2017-03-14 10:51:28 +01:00
2024-01-21 02:26:08 +01:00
if [ " $1 " = "" ] || [ " $2 " = "" ]
2015-02-22 16:01:01 +01:00
then
2024-01-21 02:26:08 +01:00
echo "Usage: odt2pdf.sh fullfilename [native|unoconv|jodconverter|pyodconverter|pathtojodconverterjar|pandoc]"
2017-10-10 18:54:30 +02:00
echo "Example: odt2pdf.sh myfile unoconv"
2015-02-22 16:01:01 +01:00
echo "Example: odt2pdf.sh myfile ~/jodconverter/jodconverter-cli-2.2.2.jar"
exit
fi
2024-01-21 02:26:08 +01:00
FILENAME = $1
METHOD = $2
2017-10-10 18:54:30 +02:00
2024-01-10 19:47:29 +01:00
# Full patch where soffice is installed
2024-01-21 02:26:08 +01:00
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 " )
2017-03-14 10:51:28 +01:00
2024-01-10 19:47:29 +01:00
# 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.
2024-01-21 02:26:08 +01:00
home_java = ${ HOME_JAVA : =/tmp }
2013-04-07 17:27:35 +02:00
2017-03-14 10:51:28 +01:00
# Main program
2024-01-21 02:26:08 +01:00
if [ -f " ${ FILENAME } .odt " ]
2017-10-10 18:54:30 +02:00
then
2024-01-21 02:26:08 +01:00
if [ " ${ METHOD } " = = "native" ]
2024-01-10 19:47:29 +01:00
then
2024-01-21 02:26:08 +01:00
" $soffice " --headless " -env:UserInstallation=file:/// $home_java / " --convert-to pdf:writer_pdf_Export --outdir " $( dirname " ${ FILENAME } " ) " " ${ FILENAME } .odt "
2024-01-10 19:47:29 +01:00
exit 0
fi
2019-12-30 17:25:54 +01:00
2024-01-21 02:26:08 +01:00
if [ " ${ METHOD } " = = "unoconv" ]
2024-01-10 19:47:29 +01:00
then
# See issue https://github.com/dagwieers/unoconv/issues/87
2024-01-21 02:26:08 +01:00
/usr/bin/unoconv -vvv " ${ FILENAME } .odt "
retcode = $?
if [ $retcode -ne 0 ]
then
echo " Error while converting odt to pdf: $retcode "
exit 1
fi
exit 0
fi
if [ " ${ METHOD } " = = "pandoc" ]
then
pandoc " ${ FILENAME } .odt " -t pdf -o " ${ FILENAME } .pdf "
2024-01-10 19:47:29 +01:00
retcode = $?
if [ $retcode -ne 0 ]
then
echo " Error while converting odt to pdf: $retcode "
exit 1
fi
exit 0
fi
2017-10-10 18:54:30 +02:00
2024-01-21 02:26:08 +01:00
nbprocess = $( pgrep -c " $soffice_basename " )
if [ " $nbprocess " -ne 1 ] # If there is some soffice process running
2024-01-10 19:47:29 +01:00
then
2024-01-21 02:26:08 +01:00
# 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 " &
2024-01-10 19:47:29 +01:00
retcode = $?
if [ $retcode -ne 0 ]
then
echo " Error running soffice: $retcode "
exit 1
fi
sleep 2
fi
2024-01-21 02:26:08 +01:00
if [ " ${ METHOD } " = = "jodconverter" ]
2024-01-10 19:47:29 +01:00
then
2024-01-21 02:26:08 +01:00
jodconverter " ${ FILENAME } .odt " " ${ FILENAME } .pdf "
2024-01-10 19:47:29 +01:00
else
2024-01-21 02:26:08 +01:00
if [ " ${ METHOD } " = = "pyodconverter" ]
2024-01-10 19:47:29 +01:00
then
2024-01-21 02:26:08 +01:00
python DocumentConverter.py " ${ FILENAME } .odt " " ${ FILENAME } .pdf "
2024-01-10 19:47:29 +01:00
else
2024-01-21 02:26:08 +01:00
java -jar " ${ METHOD } " " ${ FILENAME } .odt " " ${ FILENAME } .pdf "
2024-01-10 19:47:29 +01:00
fi
fi
retcode = $?
if [ $retcode -ne 0 ]
then
echo " Error while converting odt to pdf: $retcode "
exit 1
fi
sleep 1
2017-10-10 18:54:30 +02:00
else
2024-01-21 02:26:08 +01:00
echo " Error: Odt file ${ FILENAME } .odt does not exist "
2024-01-10 19:47:29 +01:00
exit 1
2013-04-07 17:27:35 +02:00
fi