-
Notifications
You must be signed in to change notification settings - Fork 92
/
generate-booklet
111 lines (92 loc) · 4.49 KB
/
generate-booklet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
# ---------------------------------------------------------
# Imposition of a document to generate a PDF booklet
# target format is selectable (A5, A4, ...) and default format is A4
#
# Setup procedure : http://bernaerts.dyndns.org/linux/74-ubuntu/248-ubuntu-imposition-print-book
#
# 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
# ---------------------------------------------------------
# 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; }
# suffix to be added at the end of booklet file
SUFFIX="book"
# check if parameter is given, otherwise open dialog box selection
[ "$1" != "" ] && FILE="$1" || FILE=$(zenity --file-selection --title="Select document to convert to booklet")
# if a document has been selected, start conversion
if [ "${FILE}" != "" ];
then
# document type undefined
TYPE=""
# booklet name will be originalname-book.pdf
BOOKLET="$(echo "${FILE}" | sed 's/\(.*\)\..*/\1/')-${SUFFIX}.pdf"
FILETMP="$(echo "${FILE}" | sed 's/\(.*\)\..*/\1/')-tmp.pdf"
# get the file mime type (application/msword, ...)
MIMETYPE=$(mimetype -b "${FILE}")
# 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"
# check specific document types
case $MIMETYPE in
# ms-office document (.doc, .xls, .ppt, ...)
"application/msword" | "application/vnd.ms-word" | "application/vnd.ms-powerpoint" )
TYPE="ms-office"
;;
# PDF document (.pdf)
"application/pdf" | "application/x-pdf" | "application/x-bzpdf" | "application/x-gzpdf" )
TYPE="pdf"
;;
# plain text file (.txt)
"text/plain" | "application/x-shellscript" )
TYPE="text"
;;
# other mimetype
* )
;;
esac
# if document type is not recognised as candidate, 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}")
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 a format has been selected, start booklet conversion process
(
if [ "$FORMAT" != "" ]
then
# set target format as expected by pdfbook
OPTION="--${FORMAT,,}paper"
# 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 "${FILETMP}" "${FILE}" || FILETMP="${FILE}"
# 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 "${FILETMP}" | grep Pages | sed 's/^Pages:[ ]*\([0-9]*\).*$/\1/g')
DIVPAGES=$(expr $NUMPAGES % 4)
[ "$DIVPAGES" -gt "0" ] && NUMPAGES=$(expr $NUMPAGES + 4 - $DIVPAGES)
# generate document with proper page number (multiple of 4)
echo "# Generation of PDF booklet"
pdfbook ${OPTION} --signature ${NUMPAGES} --booklet true --landscape "${FILETMP}" -o "${BOOKLET}"
fi
# if file is not PDF, remove temporary PDF file
[ "${TYPE}" != "pdf" ] && rm "${FILETMP}"
) | zenity --width=500 --height=25 --progress --pulsate --auto-close --title "${FORMAT} booklet generation"
fi