Converting MS Office Documents to PDF
Sometimes you have a Microsoft Office document, say an MS Word or MS PowerPoint file, and want to convert it to PDF.
If you do not have Microsoft Office installed, then you can do this via LibreOffice, which is free open source.
Here I provide the little Bash script office2pdf.sh, which does this in the Linux terminal.
It basically just executes LibreOffice with the correct parameters.
All you have to do is to provide the path to the office document as parameter to the script.
It will then create a PDF with the same base name in the current folder, e.g., office2pdf.sh mydoc.docx creates the file mydoc.pdf in the current folder.
This should work with extensions such as .doc, .docx, .ppt, .pptx, .xls, and .xlsx.
Additionally, you can also provide a destination path as optional second argument.
office2pdf.sh mydoc.docx ../result.pdf creates the document result.pdf in the parent folder of the current folder.
The script will check if LibreOffice is installed and abort if not.
In that case, an appropriate error message is printed.
You can install LibreOffice via sudo apt-get install libreoffice.
The script will also fail with an error if either the input file does not exist or if the expected output file is not produced for some reason.
Here you can download this script and the complete collection of my personal scripts is available here.
In that collection, there also is a script convertTo.sh, which bundles different conversion scripts between different formats.
It decides based on the file extensions which scripts to use (or fails if no fitting script is available).
Another related script is office2pdf.sh.#!/bin/bash -
# Convert an MS Office document (doc, docx, xls, xlsx, ppt, pptx, ...) to pdf.
#
# The script expects the following parameters:
# 1. The path to an office document, with an extension like those above.
# 2. OPTIONAL: the path to the destination document.
#
# If the destination path is not provided, it will create a document with the
# same name but .pdf as extension in the current directory.
# The conversion may not preserve some images correctly, but it more or less
# works.
#
# This script is basically a wrapper around LibreOffice.
# strict error handling
set -o pipefail # trace ERR through pipes
set -o errtrace # trace ERR through 'time command' and other functions
set -o nounset # set -u : exit the script if you try to use an uninitialized variable
set -o errexit # set -e : exit the script if any statement returns a non-true return value
if [ $# -lt 1 ]; then
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Convert an MS Office document of PDF."
echo "Parameters:"
echo " 1. path to the source MS Office document"
echo " 2. OPTIONAL: Path to destination document"
exit 1
fi
if ! ( command -v libreoffice &> /dev/null ); then
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): LibreOffice is not installed but needed."
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): You can install it via 'sudo apt-get install libreoffice'."
exit 1
fi
srcDocument="$(realpath "$1")"
if [ -f "$srcDocument" ]; then
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Got source document '$srcDocument'."
else
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Source document $srcDocument' does not exist."
exit 1
fi
dstDocument="${2:-}"
if [[ -n "$dstDocument" ]]; then
dstDocument="$(realpath $dstDocument)"
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Converting '$srcDocument' to the specified destination document '$dstDocument'."
else
dstDocument="$(basename "${srcDocument%.*}.pdf")"
dstDocument="$(realpath $dstDocument)"
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): No destination document specified, therefore converting '$srcDocument' to '$dstDocument'."
fi
tempDir="$(mktemp -d)"
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Using temporary directory '$tempDir'."
outDocument="$(basename "${srcDocument%.*}.pdf")"
outPath="$(realpath "$tempDir/$outDocument")"
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Now converting '$srcDocument' to '$outPath'."
libreoffice --headless --safe-mode --convert-to pdf "$srcDocument" --outdir "$tempDir"
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Moving '$outPath' to '$dstDocument'."
mv "$outPath" "$dstDocument"
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Removing '$tempDir'."
rm -d "$tempDir"
if [ -f "$dstDocument" ]; then
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Finished converting '$srcDocument' to '$dstDocument'."
else
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Destination document '$dstDocument' was not created."
exit 1
fi