ubuntuusers.de

Anhang: merge2PDF

  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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/bin/bash
#
################################################################################
#  
#  merge2PDF V-1.0
#  
#  This tool use ImageMagick to convert Images and PDFs into one PDF File.
#
################################################################################
#  
#  Copyright (C) 2011 by Johannes Storm <jo-master@gmx.de>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#  General Public License for more details.
#  
#  
#  use:
#  - copy this script in Nautilus Script folder and make it executable
#  - in Nautilus select the files and run this script from context menu
#  
#  
#  required software:
#  - ImageMagick
#  - zenity
#  - coreutils
#  
################################################################################

# variables
TMPDIRNAME="merged_output"
FILENAME="merged_output.pdf"


# check for zenity
gui=$(which zenity)
if [ ! -x "$gui" ];
then
    echo "Zenity is required: 'sudo apt-get install zenity"
    exit 0
fi


# check for imagemagick
convertcmd=$(which convert)
if [ ! -x "$convertcmd" ];
then
    zenity --error --title="Missing Software" \
           --text="The required software 'ImageMagick' couldn't found"
    exit 0
fi
compositecmd=$(which composite)
if [ ! -x "$compositecmd" ];
then
    zenity --error --title="Missing Software" \
           --text="The required software 'ImageMagick' couldn't found"
    exit 0
fi


# ask for filename
FOUND=0
while [ $FOUND -eq 0 ];
do
    FILENAME=$(zenity --entry \
                      --title="Filename" \
                      --text="Enter Filename" \
                      --entry-text="${FILENAME}" \
                      --width="300")
    if [ -z "${FILENAME}" ];
    then
        exit 0
    elif [ -e "${FILENAME}" ];
    then
        zenity --question --text="The file already exists. Overwrite it?"
        if [ $? = 0 ];
        then
            rm -f "${FILENAME}"
            FOUND=1
        fi
    else
        FOUND=1
    fi
done


# ask for page type
PAGETYPE=$(zenity --list \
                  --title="Page Type" \
                  --text="Select a page type for the PDF file" \
                  --radiolist \
                  --width="300" \
                  --height="400" \
                  --column="" --column="Page Type" --column="Page Size" \
                  'TRUE' "-" "unchanged" \
                  'FALSE' "userdefined format" "" \
                  'FALSE' "11x17"  "792 x 1224" \
                  'FALSE' "Ledger"  "1224 x 792" \
                  'FALSE' "Legal"  "612 x 1008" \
                  'FALSE' "Letter"  "612 x 792" \
                  'FALSE' "LetterSmall"  "612 x 792" \
                  'FALSE' "ArchE"  "2592 x 3456" \
                  'FALSE' "ArchD"  "1728 x 2592" \
                  'FALSE' "ArchC"  "1296 x 1728" \
                  'FALSE' "ArchB"  "864 x 1296" \
                  'FALSE' "ArchA"  "648 x 864" \
                  'FALSE' "A0"  "2380 x 3368" \
                  'FALSE' "A1"  "1684 x 2380" \
                  'FALSE' "A2"  "1190 x 1684" \
                  'FALSE' "A3"  "842 x 1190" \
                  'FALSE' "A4"  "595 x 842" \
                  'FALSE' "A4Small"  "595 x 842" \
                  'FALSE' "A5"  "421 x 595" \
                  'FALSE' "A6"  "297 x 421" \
                  'FALSE' "A7"  "210 x 297" \
                  'FALSE' "A8"  "148 x 210" \
                  'FALSE' "A9"  "105 x 148" \
                  'FALSE' "A10"  "74 x 105" \
                  'FALSE' "B0"  "2836 x 4008" \
                  'FALSE' "B1"  "2004 x 2836" \
                  'FALSE' "B2"  "1418 x 2004" \
                  'FALSE' "B3"  "1002 x 1418" \
                  'FALSE' "B4"  "709 x 1002" \
                  'FALSE' "B5"  "501 x 709" \
                  'FALSE' "C0"  "2600 x 3677" \
                  'FALSE' "C1"  "1837 x 2600" \
                  'FALSE' "C2"  "1298 x 1837" \
                  'FALSE' "C3"  "918 x 1298" \
                  'FALSE' "C4"  "649 x 918" \
                  'FALSE' "C5"  "459 x 649" \
                  'FALSE' "C6"  "323 x 459" \
                  'FALSE' "Flsa"  "612 x 936" \
                  'FALSE' "Flse"  "612 x 936" \
                  'FALSE' "HalfLetter"  "396 x 612")
if [ -z "${PAGETYPE}" ];
then
    exit 0
elif [ "${PAGETYPE}" = "-" ];
then
    convert "$@" "${FILENAME}"
    exit 0
elif [ "${PAGETYPE}" = "userdefined format" ];
then
    PAGEFORMAT=$(zenity --entry \
                        --title="Page Type" \
                        --text="Enter a page format for the PDF file ('Width'x'Height') [px]" \
                        --entry-text="595x842" \
                        --width="300")
    if [ -z "${PAGEFORMAT}" ];
    then
        exit 0
    else
        PAGETYPECMD="-page ${PAGEFORMAT}"
        PAGEROTATE=0
    fi
else
    PAGETYPECMD="-page ${PAGETYPE}"
    
    PAGEFORMAT=$(zenity --list \
                        --title="Page Type" \
                        --text="Select a page format for the PDF file" \
                        --radiolist \
                        --width="300" \
                        --column="" --column="Page Type" \
                        'TRUE' "vertical format" \
                        'FALSE' "horizontal format")
    if [ -z "${PAGEFORMAT}" ];
    then
        exit 0
    elif [ "${PAGEFORMAT}" = "vertical format" ];
    then
        PAGEROTATE=0
    else
        PAGEROTATE=1
    fi
fi


# get width and height of page type
PAGESTYLES="11x17 - 792 x 1224
Ledger - 1224 x 792
Legal - 612 x 1008
Letter - 612 x 792
LetterSmall - 612 x 792
ArchE - 2592 x 3456
ArchD - 1728 x 2592
ArchC - 1296 x 1728
ArchB - 864 x 1296
ArchA - 648 x 864
A0 - 2380 x 3368
A1 - 1684 x 2380
A2 - 1190 x 1684
A3 - 842 x 1190
A4 - 595 x 842
A4Small - 595 x 842
A5 - 421 x 595
A6 - 297 x 421
A7 - 210 x 297
A8 - 148 x 210
A9 - 105 x 148
A10 - 74 x 105
B0 - 2836 x 4008
B1 - 2004 x 2836
B2 - 1418 x 2004
B3 - 1002 x 1418
B4 - 709 x 1002
B5 - 501 x 709
C0 - 2600 x 3677
C1 - 1837 x 2600
C2 - 1298 x 1837
C3 - 918 x 1298
C4 - 649 x 918
C5 - 459 x 649
C6 - 323 x 459
Flsa - 612 x 936
Flse - 612 x 936
HalfLetter - 396 x 612"

if [ "${PAGETYPE}" = "userdefined format" ];
then
    PAGEWIDTH=$(echo "${PAGEFORMAT}" | sed -e "s/\(.*\)x\(.*\)/\1/")
    PAGEHEIGHT=$(echo "${PAGEFORMAT}" | sed -e "s/\(.*\)x\(.*\)/\2/")
else
    PAGEWIDTH=$(echo "${PAGESTYLES}" | grep "${PAGETYPE} - " | \
                sed -e "s/${PAGETYPE} - \(.*\) x \(.*\)/\1/")
    PAGEHEIGHT=$(echo "${PAGESTYLES}" | grep "${PAGETYPE} - " | \
                 sed -e "s/${PAGETYPE} - \(.*\) x \(.*\)/\2/")
fi

if [ $PAGEROTATE -eq 1 ];
then
    TMP=$PAGEWIDTH
    PAGEWIDTH=$PAGEHEIGHT
    PAGEHEIGHT=$TMP
fi


# ask for borders
BORDERSTR=$(zenity --entry \
                   --title="Border" \
                   --text="Enter size of borders if required ('Width'x'Height') [px]" \
                   --entry-text="50x50" \
                   --width="300")
if [ -z "${BORDERSTR}" ];
then
    BORDERWIDTH=0
    BORDERHEIGHT=0
else
    BORDERWIDTH=$(echo "${BORDERSTR}" | sed -e "s/\(.*\)x\(.*\)/\1/")
    BORDERHEIGHT=$(echo "${BORDERSTR}" | sed -e "s/\(.*\)x\(.*\)/\2/")
fi
IMGAWIDTH=$((PAGEWIDTH-2*BORDERWIDTH))
IMGAHEIGHT=$((PAGEHEIGHT-2*BORDERHEIGHT))


# ask for quality
QUALITY=$(zenity --scale \
                 --title="Image Quality" \
                 --text="Select a quality for compression [%]" \
                 --min-value=1 \
                 --max-value=100 \
                 --step=1 \
                 --value=92)
if [ -z "${QUALITY}" ];
then
    exit 0
fi


# convert files
mkdir -p "${TMPDIRNAME}/images"
mkdir -p "${TMPDIRNAME}/pdfs"

exec 3> >(zenity --progress --title="Convert ..." --pulsate --auto-close)

ERRMSG=$(
    convert xc:white ${PAGETYPECMD} "${TMPDIRNAME}/white.pdf" 2>&1
    if [ $PAGEROTATE -eq 1 ];
    then
        convert "${TMPDIRNAME}/white.pdf" -rotate 90 \
                "${TMPDIRNAME}/white.pdf" 2>&1
    fi
    for FILE in "$@"
    do
        HEIGHT=$(identify -format "%[fx:h]" "${FILE}")
        WIDTH=$(identify -format "%[fx:w]" "${FILE}")
        
        if [ $(($IMGAWIDTH*100/$WIDTH)) -le $(($IMGAHEIGHT*100/$HEIGHT)) ];
        then
            NEWWIDTH=$IMGAWIDTH
            NEWHEIGHT=$(($HEIGHT*$IMGAWIDTH/$WIDTH))
        else
            NEWWIDTH=$(($WIDTH*$IMGAHEIGHT/$HEIGHT))
            NEWHEIGHT=$IMGAHEIGHT
        fi
        
        if [ $NEWWIDTH -le $WIDTH -o $NEWHEIGHT -le $HEIGHT ];
        then
            convert "${FILE}" -scale "${NEWWIDTH}x${NEWHEIGHT}" \
                    -quality "${QUALITY}" \
                    "${TMPDIRNAME}/images/${FILE}" 2>&1
        else
            cp "${FILE}" "${TMPDIRNAME}/images/${FILE}"
        fi
        composite -gravity center "${TMPDIRNAME}/images/${FILE}" \
                  "${TMPDIRNAME}/white.pdf" \
                  "${TMPDIRNAME}/pdfs/${FILE}.pdf" 2>&1
    done
    convert ${TMPDIRNAME}/pdfs/* "${FILENAME}" 2>&1
)
rm -rf "${TMPDIRNAME}"
exec 3>&-


# error message
if [ ! -z "${ERRMSG}" ];
then
    zenity --error \
           --title="An error has occured" \
           --text="There was an error during converting to PDF-File."
    echo "${ERRMSG}" | zenity --text-info \
                              --title "Error Message" \
                              --width="500" \
                              --height="500"
fi


exit 0

Anhang herunterladen

Diese Revision wurde am 29. Januar 2015 17:54 von ubuntuusers erstellt.