ReorderPDF1342-new.sh

RĂ©mi G., 06/01/2014 04:26 pm

Download (979 Bytes)

 
1
#!/bin/bash
2
# Create the destination file and temporary files
3
file=original53.pdf;
4
newfile=$(basename $file .pdf)-2.pdf;
5
tmpfile=$(mktemp --suffix=.pdf);
6
blankfile=$(mktemp --suffix=.pdf);
7
cp -f $file $newfile;
8
cp -f $file $tmpfile;
9
10
# Find the number of pages
11
numberofpages=`pdftk $file dump_data | grep "NumberOfPages" | sed 's:.*\([0-9][0-9*]\).*:\1:'`;
12
13
# Insert blank pages until the number of pages is a multiple of 4
14
echo "" | ps2pdf -sPAPERSIZE=a4 - $blankfile;
15
while (( $numberofpages % 4 != 0 )); # DIFFERENCE: %% instead of % since % is used by Nautilus-Actions for special parameters
16
do 
17
  ((numberofpages++));
18
  pdftk A=$newfile B=$blankfile cat A B output $tmpfile;
19
  cp -f $tmpfile $newfile;
20
done;
21
22
# Reorder every 4 pages of a PDF following the permutation 1234 -> 1342
23
a=0;
24
neworder=$(
25
  while [ $a -lt $numberofpages ];
26
  do 
27
    echo -n "$(($a + 1)) $(($a + 3)) $(($a + 4)) $(($a + 2)) ";
28
    ((a+=4));
29
  done;
30
);
31
pdftk $tmpfile cat $neworder output $newfile;