Ubuntu - Imposition or how to print a book from your documents



ubuntu-imposition-bookIn daily office life, who has not thought while printing a document :

It's a waste to print that document on A4 pages.
It would have been so handy to get it in a small A5 booklet.

Imposition is the clue !

Imposition is the process of arranging pages on printer's sheet, in order to obtain faster printing, simplified binding and reduced paper waste.

This guide will explain how to achieve that simple process of converting a plain document in portrait mode into a small A5 booklet printed on recto-verso A4 pages.

As an input, it takes a an LibreOffice, Ms-Office or PDF document and generates a PDF document ready for printing.

By default you will generate a document ready for printing on A4 pages, but some other formats like A3, A5, Letter, ... are also available (thanks to Robin idea).

It has been tested on Ubuntu 12.04 LTS, Ubuntu Gnome and Lubuntu 14.04 LTS, but it should work on any Linux flavor as long as you get pdfinfo, pdfbook and unoconv utilities.

If you don't need any technical explanation and you just want to be able to generate a PDF booklet straight from Nautilus file manager, you can jump to Complete installation procedure. It will provide a complete and simple installation script.

1. Some Explainations

Before going to the scripting part, lets understand the process.

Booklet Imposition main goal is simplicity. You should get :

  • no cutting
  • pages folded in half lengthwise
  • pages stapled centerline

On every recto-verso A4 pages you will print 4 pages of the original document :

  • The first and last pages are printed on one side of a standard A4 page
  • The second and penultimate page on the other side of that same A4 page
  • The page should be printed recto-verso on the long-edge

You have to repeat that first/last & second/penultimate relationship going thru the document, until you reach the middle of the document.


You can now simply fold in half lengthwise the printed pages and staple the binding line.

The result is a A5 booklet printed on A4 pages.

2. Install Packages

The booklet generation script will need four specific tools :

  • pdfinfo : to get number of pages from original document
  • pdfbook : to do the imposition job
  • mimetype : to determine the type of input document
  • unoconv : to convert an office document to a PDF file

Under Ubuntu, these tools are provided by the following packages (valid for Precise 12.04 and Trusty 14.04) :

# sudo apt-get install poppler-utils texlive-extra-utils mimetype unoconv

3. Imposition Script

The complete imposition job is done by a shell script.

This script takes the original document as a parameter. It adds blank pages at the end of the booklet if needed.

If no parameter is given, the scripts asks to select an input document.

Booklet is generated side to the original document, having the same name with -book appended at the end.

#!/usr/bin/env bash
# ---------------------------------------------------------
# Imposition of a document to generate a PDF booklet
# target format is selectable (A5, A4, ...) and default format is A4
# Setup procedure :
# Depends on packages : poppler-utils, texlive-extra-utils, unoconv and mimetype
# Parameter :
#   $1 - full path of original document
# Version history :
#   26/09/2012, V1.0 - Creation by N. Bernaerts
#   05/05/2014, V2.0 - Add target format selection (thanks to Robin idea)
#   16/12/2014, V2.1 - Add file selection dialog
#   10/05/2015, V2.2 - Check tools availability
#   03/11/2015, V3.0 - Add support for LibreOffice & Ms-Office documents
#   18/10/2017, V3.1 - Correct critical bug (thanks to Igor Kushnir)
# ---------------------------------------------------------

# check mimetype, pdfinfo, pdfbook and unoconv
command -v mimetype >/dev/null 2>&1 || { zenity --error --text="Please install mimetype"; exit 1; }
command -v pdfinfo >/dev/null 2>&1 || { zenity --error --text="Please install pdfinfo [poppler-utils package]"; exit 1; }
command -v pdfbook >/dev/null 2>&1 || { zenity --error --text="Please install pdfbook [texlive-extra-utils package]"; exit 1; }
command -v unoconv >/dev/null 2>&1 || { zenity --error --text="Please install unoconv"; exit 1; }

# check if parameter is given, otherwise open dialog box selection
[[ "$1" != "" ]] && FILE_ORIGINAL="$1" || FILE_ORIGINAL=$(zenity --file-selection --title="Select document to convert to booklet")

# if no document selected, exit
[[ ! -f "${FILE_ORIGINAL}" ]] && exit 1

# booklet name will be originalname-book.pdf
FILE_BOOKLET="$(echo "${FILE_ORIGINAL}" | sed 's/\(.*\)\..*/\1/')-book.pdf"

# get the file mime type (application/msword, ...)
MIMETYPE=$(mimetype -b "${FILE_ORIGINAL}")

# check if file is a libreoffice file (.odt, .ods, ...)
CHECK=$(echo "${MIMETYPE}" | grep ".opendocument.")
[[ "${CHECK}" != "" ]] && TYPE="libreoffice"

# check if file is a microsoft file 2007+ file (.docx, .xlsx, .pptx, ...)
CHECK=$(echo "${MIMETYPE}" | grep "vnd.openxmlformats-officedocument.")
[[ "${CHECK}" != "" ]] && TYPE="ms-office"

# if type is not found, check specific document types
if [[ "${TYPE}" = "" ]]
    case ${MIMETYPE} in 
    # ms-office document (.doc, .xls, .ppt, ...)
    "application/msword" | "application/" | "application/" )

    # PDF document (.pdf)
    "application/pdf" | "application/x-pdf" | "application/x-bzpdf" | "application/x-gzpdf" )

    # plain text file (.txt)
    "text/plain" | "application/x-shellscript" )

    # other mimetype
    * )

# if document type is not a candidate one, exit
[[ "${TYPE}" = "" ]] && { zenity --error --text="Document format is not compatible (pdf, libreoffice or ms-office)"; exit 1; }

# select target format
ARR_FORMAT=('false' 'A2' 'false' 'A3' 'true' 'A4' 'false' 'A5' 'false' 'Letter' 'false' 'Executive' 'false' 'Legal')
TITLE=$(basename "${FILE_ORIGINAL}")
TEXT="Booklet will be generated from this document.\nPlease select target format.\n"
FORMAT=$(zenity --list --radiolist --width 350 --height 380 --title "${TITLE}" --text "${TEXT}" --column="Choice" --column="Format" ${ARR_FORMAT[@]})

# if no format selected, exit
[[ "${FORMAT}" = "" ]] && exit 1

# --------------------
#  booklet conversion 
# --------------------
# temporary file
FILE_TMP=$(mktemp -t XXXXXXXX.pdf) && rm "${FILE_TMP}" || exit 2
# if file is not PDF, convert file to pdf, else use original
echo "# Conversion of original document to PDF format"
[[ "${TYPE}" != "pdf" ]] && unoconv -f pdf -o "${FILE_TMP}" "${FILE_ORIGINAL}"

# set conversion source as temporary PDF or original PDF
# calculate number of pages of original PDF 
# and determine if blank pages to be added at the end to get multiple of 4
echo "# Calculate number of booklet pages"
NUMPAGES=$(pdfinfo "${FILE_SOURCE}" | grep Pages | sed 's/^Pages:[ ]*\([0-9]*\).*$/\1/g')
DIVPAGES=$(expr ${NUMPAGES} % 4)
[[ "${DIVPAGES}" -gt "0" ]] && NUMPAGES=$(expr ${NUMPAGES} + 4 - ${DIVPAGES})

# set target format as expected by pdfbook

# generate document with proper page number (multiple of 4)
echo "# Generation of PDF booklet"
pdfbook ${OPTION} --signature ${NUMPAGES} --booklet true --landscape "${FILE_SOURCE}" -o "${FILE_BOOKLET}"

# remove temporary PDF file (may exist or not)
rm -f "${FILE_TMP}"
) | zenity --width=500 --height=25 --progress --pulsate --auto-close --title "${FORMAT} booklet generation"

You can use following commands to install the script and declare it into your utilities main menu.

# sudo wget --header='Accept-Encoding:none' -O /usr/local/bin/generate-booklet
# sudo chmod +x /usr/local/bin/generate-booklet
# sudo wget --header='Accept-Encoding:none' -O /usr/share/applications/generate-booklet.desktop
# sudo chmod +x /usr/share/applications/generate-booklet.desktop

4. Access from Nautilus Menu

If you want your imposition script to be accessible straight from a right click menu on a LibreOffice, Ms-Office or PDF file in Nautilus, this can be done quite easily with Nautilus Actions.

If not already done, install Nautilus Action package and allow context menu icons :

# sudo apt-get install nautilus-actions
# gsettings set org.gnome.desktop.interface menus-have-icons true

You are now ready to declare a new Nautilus action menu.

This action is declared by creating a specific .desktop file under ~/.local/share/file-manager/actions directory (which may not exist).

Action file declares to Nautilus the action to be launched according to some specific mimetypes.

[Desktop Entry]
Name[C]=Generate Booklet
Name[en]=Generate Booklet
Name[en_US]=Generate Booklet
Name[fr_FR]=Générer un livret
Tooltip[C]=Tool to generate a PDF booklet
Tooltip[en]=Tool to generate a PDF booklet
Tooltip[en_US]=Tool to generate a PDF booklet
Tooltip[fr_FR]=Outil de génération d'un livret au format PDF

[X-Action-Profile generate_booklet]
Exec=generate-booklet %f
Name[en_US]=Default profile
Name[en]=Default profile
Name[C]=Default profile

Following command creates action directory if it doesn't exist and declares Nautilus action :

# mkdir --parents $HOME/.local/share/file-manager/actions
# wget --header='Accept-Encoding:none' -O $HOME/.local/share/file-manager/actions/generate-booklet-action.desktop

All the needed steps are done to get a new  Generate booklet menu entry for PDF documents.

5. Complete installation procedure

If you want to install all needed tools and scripts in one go, you can run an all-in-one installation script available from my Github repository.

This script has been written and tested on Ubuntu 14.04 LTS. It will handle whatever installation and configuration described earlier in this article.

# wget --header='Accept-Encoding:none'
# chmod +x
# ./

6. Generate your PDF booklets

ubuntu-imposition-nautilus-menuAfter next login you should get a new  Generate booklet menu entry for PDF documents.

Everytime you select this menu on a PDF document, a A5 booklet is generated side to the original document.

You can now print it on any recto-verso printer in portrait mode, you will get your A5 booklet.

In case you detect any bug or if you have some update ideas which can benefit everybody, don't hesitate to contact me by email or to fork it on GitHub.


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