Search

Nautilus - Thumbnailer for LibreOffice documents

Contents[Hide]

dropcap-gnome-libreoffice

When you use Gnome Shell under Ubuntu or Debian, all LibreOffice files are displayed thru some generic icons in Nautilus.

openoffice-iconLibreOffice files are either a ZIP archives embedding a thumbnail of the first document page(.odt, .odx, ...) or a flat XML file (.fodt, .fodx, ...). With both type of documents, it is possible to use or to generate this first page thumbnail as a Nautilus thumbnail.

To make it even more visual, the thumbnail can be outdrawn by the document type official icon. You will then get a display much more visual.

This article explains all the steps needed to configure Nautilus to display thumbnails of LibreOffice documents including document type icon and first page content, providing a simple and efficient LibreOffice documents preview. It is using some very basic tools.

This procedure has been tested under Ubuntu 18.10, Ubuntu 18.04 LTS and Ubuntu Gnome 16.04 LTS. It should be applicable to many Gnome based distributions.

Here is the display you get in Nautilus with the generic icons :

nautilus-libreoffice-nothumbnail

And, here is the display you will get with the OpenOffice thumbnailer active :

nautilus-libreoffice-thumbnail

If you just want to install the LibreOffice thumbnailer on your system, without any specific explanation, you can jump to Thumbnailer Installation.

1. Pre-requisite

The thumbnailer script uses different tools :

  • libreoffice (to convert first document page to PNG)
  • unzip (to extract document thumbnails)
  • mimetype (to detect mimetype of libreoffice documents)
  • netpbm tools (to convert, resize and generate thumbnails)

Under Ubuntu, these tools can be easily installed :

Terminal
# sudo apt-get install libfile-mimeinfo-perl unzip netpbm

2. Thumbnailer Script

When Nautilus needs to generate a thumbnail for a file, it is calling a 'thumbnailer'. This program (or script) is in charge of generating the thumbnail in a PNG format with a given height.

LibreOffice standard files (.odt, .odx, ...) are in fact ZIP archives and the archive is embedding by default a thumbnail of the first document page, first step is to extract the embedded thumbnail from the archive. It is always stored under Thumbnails/thumbnail.png.

LibreOffice flat XML files (.fodt, .fodx, ...) do not contain any embedded thumbnail. So 1st page of the document needs to be converted. Conversion is handled by LibreOffice itself in headless mode. This mode allows libreoffice binary to be called in console mode without any GUI.  To avoid any conflict with a running graphical instance of LibreOffice, binary is called in a temporary user environment to do the conversion.

Once extracted or generated, thumbnail can be transformed with a specific mask and then applied on the original document type icon.

After resizing, this will give the final Nautilus thumbnail.

nautilus-thumbnailer-chain

The following script is a LibreOffice document 'thumbnailer' doing this job :

/usr/local/sbin/lo-thumbnailer
#!/bin/bash
# ---------------------------------------------------
# Thumbnailer for LibreOffice & OpenOffice documents
#
# Procedure :
#   http://bernaerts.dyndns.org/linux/76-gnome/285-gnome-shell-generate-libreoffice-thumbnail-nautilus
# Depends on :
#   * libreoffice or soffice
#   * mimetype
#   * unzip
#   * 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 :
# 04/08/2013, V1.0 - Creation by N. Bernaerts
# 15/11/2014, V2.0 - Use URI to handle network shares
# 07/10/2017, V2.1 - Speedup with netpbm tools
# 09/03/2018, V3.0 - Add flat XML files support
# 12/03/2018, V3.1 - Handle libreoffice and soffice binaries
# 08/11/2018, V3.2 - Handle gio and gvfs-copy tools
# 11/12/2018, V3.3 - Rework for bubblewrap compatibility (thanks to Li Chong)
# ---------------------------------------------------

# check libreoffice availability
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 mimetype >/dev/null 2>&1 || { echo "[error] mimetype missing"; exit 1; }
command -v unzip >/dev/null 2>&1 || { echo "[error] unzip missing"; exit 1; }
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"

# system ressources
PATH_SYSTEM="/usr/local/sbin/lo-thumbnailer.res"
[ ! -d "${PATH_SYSTEM}" ] && { echo "[error] directory ${PATH_SYSTEM} missing"; exit 1; }

# generate temporary files and directory
TMP_DIR=$(mktemp -t -d "thumb-lo-XXXXXXXX")
TMP_PNM="${TMP_DIR}/original.pnm"
TMP_THUMB="${TMP_DIR}/thumbnail.pnm"

# get document type (text, spreadsheet, graphics or presentation)
DOC_TYPE=$(mimetype -b "${FILE_LOCAL}" | sed 's/.*: //' | sed 's/^.*opendocument.\([^-]*\).*/\1/' | cut -d'-' -f1)

# get document tag and mask
DOC_TAG="${PATH_SYSTEM}/lo-${DOC_TYPE}.pnm"
DOC_ALPHA="${PATH_SYSTEM}/lo-${DOC_TYPE}-alpha.pnm"

# if file is zipped, extract thumbnail
IS_ZIPPED=$(file -ib "${FILE_LOCAL}" | cut -d';' -f2 | grep "binary")
if [ "${IS_ZIPPED}" != "" ]
then
    # extract thumbnail from zipped document
    unzip -p "${FILE_LOCAL}" "Thumbnails/thumbnail.png" | pngtopnm > "${TMP_PNM}"

# else, file is a flat one, generate thumbnail
else
    # 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}"
fi

# generate 256x256 thumbnail with masks
pbmmake -white 208 256 | pnmcomp -align center -valign middle "${TMP_PNM}" - | pnmcomp -align center -valign middle -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}"

It is using some icons available from my GitHub repository that need to be stored under /usr/local/sbin/lo-thumbnailer.res :

  • lo-database.png
  • lo-graphics.png
  • lo-presentation.png
  • lo-spreadsheet.png
  • lo-text.png

All these icons are used by the thumbnailer script.

3. Thumbnailer Integration

To allow Nautilus to use the proper thumbnailer for LibreOffice documents, you need to declare the previous script as the thumbnailer for all the mime type of these documents (application/vnd.oasis.opendocument...).

This declaration is done via a lo.thumbnailer file placed under /usr/share/thumbnailers. This file follows a specific format, where association between the mime type and the thumbnailer is done.

A complete description of this file format is available from Gnome Developers site on the page Installing a Thumbnailer Program.

GVFS allows to generate thumbnails on remote shares very easily thru a normal path.

/usr/share/thumbnailers/lo.thumbnailer
[Thumbnailer Entry]
Exec=/bin/bash /usr/local/sbin/lo-thumbnailer %i %o %s
MimeType=application/vnd.oasis.opendocument.text;application/vnd.oasis.opendocument.text-template;application/vnd.oasis.opendocument.text-flat-xml;application/vnd.oasis.opendocument.presentation;application/vnd.oasis.opendocument.presentation-template;application/vnd.oasis.opendocument.presentation-flat-xml;application/vnd.oasis.opendocument.spreadsheet;application/vnd.oasis.opendocument.spreadsheet-template;application/vnd.oasis.opendocument.spreadsheet-flat-xml;application/vnd.oasis.opendocument.graphics;application/vnd.oasis.opendocument.graphics-template;

4. Thumbnailer Installation

A complete installation script for this LibreOffice thumbnailer is available from my GitHub repository.

You just need to run these simple commands :

Terminal
# wget https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/thumbnailer/libreoffice/lo-thumbnailer-install.sh
# chmod +x lo-thumbnailer-install.sh
# ./lo-thumbnailer-install.sh

This script also stop nautilus and purge previous thumbnails. You just need to restart Nautilus and your new thumbnails should come to life.

In case you need to purge your thumbnails cache once again, just run these commands :

Terminal
# nautilus -q
# rm -r $HOME/.cache/thumbnails

You can now enjoy to see your LibreOffice documents displayed as thumbnails.


Hope it helps.


Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus