#!/usr/bin/env bash # --------------------------------------------------- # Thumbnailer for Ms Office documents # # Procedure : # http://bernaerts.dyndns.org/linux/76-gnome/325-gnome-shell-generate-msoffice-thumbnail-nautilus # Depends on : # * libreoffice # * mimetype (libfile-mimeinfo-perl package) # * pbmmake, pngtopnm, pnmtopng, pnmscalefixed and pnmcomp (netpbm package) # Parameters : # $1 - path of office file # $2 - path of generated thumbnail # $3 - height of thumbnail in pixels # Revision history : # 11/11/2014, V1.0 - Creation by N. Bernaerts # 15/11/2014, V2.0 - Change to URI and GVFS to handle thumbnails on network shares # 31/07/2015, V2.1 - More robust unoconv command line (thanks to Vaderqk) # 07/10/2017, V2.2 - Speedup with netpbm tools # 10/03/2018, V3.0 - Switch from unoconv to libreoffice in headless mode # 15/03/2018, V3.1 - Handle libreoffice and soffice binaries # 09/12/2018, V3.2 - Rework for bubblewrap compatibility (thanks to Li Chong) # --------------------------------------------------- # check libreoffice availability (binary can be either libreoffice or soffice) CMD_OFFICE="libreoffice" command -v "${CMD_OFFICE}" >/dev/null 2>&1 || CMD_OFFICE="soffice" command -v "${CMD_OFFICE}" >/dev/null 2>&1 || { echo "[error] libreoffice missing"; exit 1; } # check tools availability command -v pbmmake >/dev/null 2>&1 || { echo "[error] pbmmake missing"; exit 1; } command -v pngtopnm >/dev/null 2>&1 || { echo "[error] pngtopnm missing"; exit 1; } command -v pnmscalefixed >/dev/null 2>&1 || { echo "[error] pnmscalefixed missing"; exit 1; } command -v pnmcomp >/dev/null 2>&1 || { echo "[error] pnmcomp missing"; exit 1; } command -v pnmtopng >/dev/null 2>&1 || { echo "[error] pnmtopng missing"; exit 1; } # check params [ "$3" = "" ] && { echo "[error] 3 params are needed : file file-thumb thumb-size"; exit 1; } # get parameters FILE_LOCAL="$1" FILE_THUMB="$2" SIZE="$3" # set working path PATH_SYSTEM="/usr/local/sbin/msoffice-thumbnailer.res" [ ! -d "${PATH_SYSTEM}" ] && { echo "[error] directory ${PATH_SYSTEM} missing"; exit 1; } # generate temporary files and directory TMP_DIR=$(mktemp -t -d "thumb-ms-XXXXXXXX") TMP_PNM="${TMP_DIR}/original.pnm" TMP_THUMB="${TMP_DIR}/thumbnail.pnm" # get the file mime type (application/msword, ...) MIMETYPE=$(mimetype -b "${FILE_LOCAL}") # determine icon type according to mime type case ${MIMETYPE} in # text "application/msword" | \ "application/vnd.ms-word" | \ "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ) DOC_TYPE="odt" ;; # spreadsheet "application/vnd.ms-excel" | \ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | \ "application/vnd.openxmlformats-officedocument.spreadsheetml.template" ) DOC_TYPE="ods" ;; # presentation "application/vnd.ms-powerpoint" | \ "application/vnd.openxmlformats-officedocument.presentationml.presentation" | \ "application/vnd.openxmlformats-officedocument.presentationml.template" | \ "application/vnd.openxmlformats-officedocument.presentationml.slideshow" ) DOC_TYPE="odp" ;; * ) ;; esac # get document tag and mask DOC_TAG="${PATH_SYSTEM}/msoffice-${DOC_TYPE}.pnm" DOC_ALPHA="${PATH_SYSTEM}/msoffice-${DOC_TYPE}-alpha.pnm" # convert first page to PNG ${CMD_OFFICE} "-env:UserInstallation=file://${TMP_DIR}" --headless --convert-to png --outdir "${TMP_DIR}" "${FILE_LOCAL}" # convert PNG to PNM FILE_NAME=$(basename "${FILE_LOCAL}") FILE_NAME="${FILE_NAME%.*}" pngtopnm "${TMP_DIR}/${FILE_NAME}.png" | pnmscalefixed -xysize 256 256 - > "${TMP_PNM}" # generate 256x256 thumbnail with masks pbmmake -white 188 256 | pnmcomp -xoff 2 -yoff=52 "${TMP_PNM}" - | pnmcomp -align left -valign top -alpha "${DOC_ALPHA}" "${DOC_TAG}" - > "${TMP_THUMB}" # downscale to final size and convert to png pnmscalefixed -xysize ${SIZE} ${SIZE} "${TMP_THUMB}" | pnmtopng -downscale - > "${FILE_THUMB}" # remove temporary directory rm -r "${TMP_DIR}"