odt2pdf.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/bin/bash
  2. # @copyright GPL License 2010 - Vikas Mahajan - http://vikasmahajan.wordpress.com
  3. # @copyright GPL License 2013 - Florian HEnry - florian.henry@open-concept.pro
  4. # @copyright GPL License 2017 - Laurent Destailleur - eldy@users.sourceforge.net
  5. # @copyright GPL License 2019 - Camille Lafitte - cam.lafit@azerttyu.net
  6. #
  7. # Convert an ODT into a PDF using "native" or "jodconverter" or "pyodconverter" or "unoconv" tool.
  8. # Dolibarr variable MAIN_ODT_AS_PDF must be defined ...
  9. # to value "libreoffice" to call soffice native exporter feature (in such a case, this script is useless)
  10. # or value "unoconv" to call unoconv CLI tool after ODT generation.
  11. # or value "pyodconverter" to call DocumentConverter.py after ODT generation.
  12. # or value "jodconverter" to call jodconverter wrapper after ODT generation
  13. # or value "/pathto/jodconverter-cli-file.jar" to call jodconverter java tool without wrapper after ODT generation.
  14. # Dolibarr variable MAIN_DOL_SCRIPTS_ROOT must be defined to path of script directories (otherwise dolibarr will try to guess).
  15. #
  16. # NOTE: Using this script is depcrecated, you can now convert generated ODT to PDF on the fly by setting the value MAIN_ODT_AS_PDF
  17. # to 'libreoffice'. It requires only soffice (OpenOffice or LibreOffice) installed on server (use apt install soffice libreoffice-common libreoffice-writer).
  18. # 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,
  19. # check that directory defined into env:UserInstallation parameters exists and is writeable.
  20. if [ "x$1" == "x" ]
  21. then
  22. echo "Usage: odt2pdf.sh fullfilename [native|unoconv|jodconverter|pyodconverter|pathtojodconverterjar]"
  23. echo "Example: odt2pdf.sh myfile unoconv"
  24. echo "Example: odt2pdf.sh myfile ~/jodconverter/jodconverter-cli-2.2.2.jar"
  25. exit
  26. fi
  27. # Full patch where soffice is installed
  28. soffice="/usr/bin/soffice"
  29. # 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.
  30. home_java="/tmp"
  31. # Main program
  32. if [ -f "$1.odt" ]
  33. then
  34. if [ "x$2" == "xnative" ]
  35. then
  36. $soffice --headless -env:UserInstallation=file:///$home_java/ --convert-to pdf:writer_pdf_Export --outdir $(dirname $1) "$1.odt"
  37. exit 0
  38. fi
  39. if [ "x$2" == "xunoconv" ]
  40. then
  41. # See issue https://github.com/dagwieers/unoconv/issues/87
  42. /usr/bin/unoconv -vvv "$1.odt"
  43. retcode=$?
  44. if [ $retcode -ne 0 ]
  45. then
  46. echo "Error while converting odt to pdf: $retcode"
  47. exit 1
  48. fi
  49. exit 0
  50. fi
  51. nbprocess=$(pgrep -c soffice)
  52. if [ $nbprocess -ne 1 ] # If there is some soffice process running
  53. then
  54. cmd="$soffice --invisible --accept=socket,host=127.0.0.1,port=8100;urp; --nofirststartwizard --headless -env:UserInstallation=file:///$home_java/"
  55. export HOME=$home_java && cd $home_java && $cmd&
  56. retcode=$?
  57. if [ $retcode -ne 0 ]
  58. then
  59. echo "Error running soffice: $retcode"
  60. exit 1
  61. fi
  62. sleep 2
  63. fi
  64. if [ "x$2" == "xjodconverter" ]
  65. then
  66. jodconverter "$1.odt" "$1.pdf"
  67. else
  68. if [ "x$2" == "xpyodconverter" ]
  69. then
  70. python DocumentConverter.py "$1.odt" "$1.pdf"
  71. else
  72. java -jar $2 "$1.odt" "$1.pdf"
  73. fi
  74. fi
  75. retcode=$?
  76. if [ $retcode -ne 0 ]
  77. then
  78. echo "Error while converting odt to pdf: $retcode"
  79. exit 1
  80. fi
  81. sleep 1
  82. else
  83. echo "Error: Odt file $1.odt does not exist"
  84. exit 1
  85. fi