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-iconAs each LibreOffice file is a ZIP archives and is embedding by default a thumbnail of the first document page, it becomes possible to use 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 Precise 12.04 LTS and Ubuntu Gnome 14.04 LTS. It should be applicable to many Gnome based distributions. It has also been tested under Lubuntu 14.04 LTS with PCManFM.

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 :

  • gvfs-copy (to copy files from remote filesystem)
  • mimetype (to detect mimetype of libreoffice documents)
  • unzip (to extract document thumbnails)
  • netpbm tools (to convert, resize and generate thumbnails)

Under Ubuntu, these tools can be easily installed :

Terminal
# sudo apt-get install gvfs-bin 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.

As LibreOffice file 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.

Once extracted, in 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 :
#   * mimetype
#   * unzip
#   * pbmmake, pngtopnm, pnmtopng, pnmscalefixed and pnmcomp (netpbm package)
#   * gvfs-copy (gvfs-bin package)
# Parameters :
#   $1 - URI of office file
#   $2 - full 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
# ---------------------------------------------------

# check tools availability
command -v mimetype >/dev/null 2>&1 || exit 1
command -v unzip >/dev/null 2>&1 || exit 1
command -v pbmmake >/dev/null 2>&1 || exit 1
command -v pngtopnm >/dev/null 2>&1 || exit 1
command -v pnmscalefixed >/dev/null 2>&1 || exit 1
command -v pnmcomp >/dev/null 2>&1 || exit 1
command -v pnmtopng >/dev/null 2>&1 || exit 1

# path where to get icons used for generation
ICONPATH="/usr/local/sbin/lo-thumbnailer-icons"

# get parameters
SCRIPT_NAME=$(basename $0)
FILE_URI="$1"
FILE_THUMB="$2"
SIZE="$3"

# set working path
PATH_SYSTEM="/usr/local/sbin/${SCRIPT_NAME}.res"
PATH_CACHE="$HOME/.cache/${SCRIPT_NAME}"
[ ! -d "${PATH_CACHE}" ] && mkdir --parent "${PATH_CACHE}"

# get filename extension
FILE_EXT=$(echo "${FILE_URI}" | sed "s/^.*\.\(.*\)/\1/")

# generate temporary local filename
TMP_LOCAL=$(mktemp -t "XXXXXXXX.${FILE_EXT}")
TMP_PNM=$(mktemp -t "XXXXXXXX.pnm")

# if file is a remote one
URI_TYPE="${FILE_URI:0:4}"
if [ "${URI_TYPE}" = "file" ]
then
    # convert URI to local path
    FILE_PATH=$(printf '%b' "${FILE_URI//%/\\x}")

    # extract local path
    FILE_LOCAL="${FILE_PATH:7}"
else
    # check gvfs-copy availability
    command -v gvfs-copy >/dev/null 2>&1 || exit 1

    # copy input file to temporary local file
    gvfs-copy "${FILE_URI}" "${TMP_LOCAL}"
    FILE_LOCAL="${TMP_LOCAL}"    
fi

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

# if needed, create tag and mask
DOC_TAG="${PATH_CACHE}/${SIZE}-${DOC_TYPE}.pnm"
DOC_ALPHA="${PATH_CACHE}/${SIZE}-${DOC_TYPE}-alpha.pnm"
[ ! -f "${DOC_TAG}" ] && pngtopnm "${PATH_SYSTEM}/lo-${DOC_TYPE}.png" | pnmscalefixed -xysize ${SIZE} ${SIZE} - > "${DOC_TAG}"
[ ! -f "${DOC_ALPHA}" ] && pngtopnm -alpha "${PATH_SYSTEM}/lo-${DOC_TYPE}.png" | pnmscalefixed -xysize ${SIZE} ${SIZE} - > "${DOC_ALPHA}"

# generate thumbnail according to document type
unzip -p "${FILE_LOCAL}" "Thumbnails/thumbnail.png" | pngtopnm > "${TMP_PNM}"
pbmmake -white 208 256 | pnmcomp -align center -valign middle "${TMP_PNM}" - | pnmscalefixed -xysize ${SIZE} ${SIZE} - | pnmcomp -align center -valign middle -alpha "${DOC_ALPHA}" "${DOC_TAG}" - | pnmtopng -downscale -compression 0 - > "${FILE_THUMB}"

# remove temporary local file
rm "${TMP_LOCAL}" "${TMP_PNM}"

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

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.

As we want to get thumbnails even for file which are accessed thru a network share, we will use the file URI instead of it's plain filename. This will allow the thumbnailer script to handle the files thru GVFS.

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

4. Thumbnailer Installation

You can download and install the complete LibreOffice Thumbnailer environment from my GitHub repository :

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 to allow them to re-generate.

You just need to restart Nautilus.

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

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

Please note that if you run Lubuntu, thumbnails should be removed under $HOME/.thumbnails and thumbnailer will work after next reboot.

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