Reputation: 1158
I had try to make a script that divide a single pdf into n parts of NUM
pages everyone, but I'm not able to do do the second part of this script (a cycle):
#!/bin/sh
#Ask pdf filename
echo -n "Filename: "
read FILENAME
#Ask number of pages of every pdf
echo -n "Pages: "
read NUM
#Save pages number of the original pdf
PAG=$(pdftk $FILENAME.pdf dump_data|grep NumberOfPages| awk '{print $2}')
#Divide pdf into a pdf file for every page
pdftk $FILENAME.pdf burst output $FILENAME-%03d.pdf
Now I must do a cycle that merge NUM
pages into a single pdf, for example, for NUM=3:
pdftk $FILENAME-001.pdf $FILENAME-002.pdf $FILENAME-003.pdf cat output PART-1.pdf
pdftk $FILENAME-004.pdf $FILENAME-005.pdf $FILENAME-006.pdf cat output PART-2.pdf
...
pdftk $FILENAME-(PAG-2).pdf $FILENAME-(PAG-1).pdf $FILENAME-PAG.pdf cat output PART-X.pdf
Then, for every obtained PDF (PART-1.pdf
, PART-2.pdf
, ..., PART-X.pdf
) I must do an operation like this:
pdftops PART-X.pdf PART-X.ps
Someone can help me?
Upvotes: 1
Views: 234
Reputation: 46
I think a while loop is the way to go here:
#!/bin/sh
...
#Save pages number of the original pdf
PAG=$(pdftk $FILENAME.pdf dump_data|grep NumberOfPages| awk '{print $2}')
# create parts 'from'-'to' given by NUM
PAG_FROM=1; PAG_TO=$NUM
while [ "$PAG_TO" -le "$PAG" ]; do
FILE_OUT="PART-$(printf %03d $PAG_FROM)-$(printf %03d $PAG_TO)"
pdftk $FILENAME.pdf cat $PAG_FROM-$PAG_TO output $FILE_OUT.pdf
pdftops $FILE_OUT.pdf $FILE_OUT.ps
PAG_FROM=$((PAG_FROM+$NUM))
PAG_TO=$((PAG_TO+$NUM))
done
# execute pdftk on trailing pages
if [ $PAG_FROM -le $PAG ]; then
FILE_OUT="PART-$(printf %03d $PAG_FROM)-$(printf %03d $PAG)"
pdftk $FILENAME.pdf cat $PAG_FROM-$PAG output $FILE_OUT.pdf
pdftops $FILE_OUT.pdf $FILE_OUT.ps
fi
I'd recommend to change your FN-variable in the beginning to
FILENAME=$(basename $1)
, that you can easily trigger the script like so:
scriptname.sh <name of file>.pdf
Tested on a 8 pages file myself, should be working just fine!
Upvotes: 2
Reputation: 1933
There are several ways to do that. You could use your knowledge of the number of pages and then do the math to construct the gluing commands. I would choose a solution that uses the files that are sure to exist, and rely on the numbering to give you the right order:
NUM=3
pages_done=0
files=''
part=1
for file in ./$FILENAME-*
do
files="$files $file"
(( pages_done++ ))
if [[ $pages_done -eq $NUM ]]
then
echo glue $files into part-$part
pages_done=0
files=''
(( part++ ))
fi
done
# Take care of any dangling files that did not make a group of size NUM
if [[ -n $files ]]
then
echo glue $files into part-$part
fi
Upvotes: 0