2021-03-23 02:32:22 +08:00
|
|
|
#!/bin/bash
|
2021-04-20 00:36:03 +08:00
|
|
|
set -e
|
|
|
|
set -u
|
|
|
|
PATH="/bin/:/usr/bin/"
|
|
|
|
|
2021-03-23 02:48:08 +08:00
|
|
|
# Conversion of office files to Pdf using local docker bbb-soffice
|
2021-03-23 02:32:22 +08:00
|
|
|
|
2021-04-13 21:41:16 +08:00
|
|
|
# This script receives three params
|
2021-03-23 02:48:08 +08:00
|
|
|
# Param 1: Input office file path (e.g. "/tmp/test.odt")
|
|
|
|
# Param 2: Output pdf file path (e.g. "/tmp/test.pdf")
|
2021-04-13 21:41:16 +08:00
|
|
|
# Param 3: Output format (pdf default)
|
2021-03-23 02:32:22 +08:00
|
|
|
|
2021-04-20 00:36:03 +08:00
|
|
|
if (( $# == 0 )); then
|
|
|
|
echo "Missing parameter 1 (Input office file path)";
|
|
|
|
exit 1
|
|
|
|
elif (( $# == 1 )); then
|
|
|
|
echo "Missing parameter 2 (Output pdf file path)";
|
|
|
|
exit 1
|
|
|
|
fi;
|
|
|
|
|
|
|
|
|
|
|
|
#Create tmp dir for conversion
|
|
|
|
mkdir -p "/tmp/bbb-soffice-$(whoami)/"
|
|
|
|
tempDir="$(mktemp -d -p /tmp/bbb-soffice-$(whoami)/)"
|
2021-10-09 14:33:33 +08:00
|
|
|
trap 'rm -fr "$tempDir"' EXIT
|
2021-04-20 00:36:03 +08:00
|
|
|
|
2021-04-27 04:38:30 +08:00
|
|
|
source="$1"
|
|
|
|
dest="$2"
|
2021-03-23 02:32:22 +08:00
|
|
|
|
2021-04-13 21:41:16 +08:00
|
|
|
#If output format is missing, define PDF
|
|
|
|
convertTo="${3:-pdf}"
|
|
|
|
convertToParam="--convert-to $convertTo"
|
|
|
|
|
|
|
|
#If output is html, include param --writer to avoid blank page
|
|
|
|
if [ ${1: -5} == ".html" ]
|
|
|
|
then
|
|
|
|
convertToParam="$convertToParam --writer"
|
|
|
|
fi
|
|
|
|
|
2021-04-20 00:36:03 +08:00
|
|
|
cp "${source}" "$tempDir/file"
|
2021-09-28 20:33:52 +08:00
|
|
|
sudo /usr/bin/docker run --rm --memory=1g --memory-swap=1g --network none --env="HOME=/tmp/" -w /tmp/ --user=$(printf %05d `id -u`) -v "$tempDir/":/data/ -v /usr/share/fonts/:/usr/share/fonts/:ro --rm bbb-soffice sh -c "/usr/bin/soffice -env:UserInstallation=file:///tmp/ $convertToParam --outdir /data /data/file"
|
2021-04-20 00:36:03 +08:00
|
|
|
cp "$tempDir/file.$convertTo" "${dest}"
|