Script for converting a pdf to book format

Hello, excuse my English... I'm trying to do a nautilus-script to transform a normal A4 pdf to another pdf with book format, ready to be printed (double sided). I mean, the script put pages in order and also put 2 pages per horizontal A4 page (p.e.: a pdf with 8 pages would look like: 8-1, 2-7, 6-3, 4-5; and a pdf with 7 pages would be: blankPage-1, 2-7, 6-3, 4-5).

I've tried a lot of commands with no result; I don't know what I'm doing wrong but I suppose it will be all of it...

Here it is the code:

#!/bin/bash


a="pdftk $1 dump_data output | grep -i Num | cut -d \  -f2"
x=a%2
y=((a+1)/2)+1
i=2
j=$a

if [ "$x" = "0" ]; then
    pdfnup $1 --nup 2x1 --pages $j,1 --outfile partwkj1.pdf
    j=j-1
else
    pdfnup $1 --nup 2x1 --pages ,1 --outfile partwkj1.pdf
fi

while [ $i -lt $y ]; do
    pdfnup $1 --nup 2x1 --pages $i,$j --outfile partwkj$i.pdf
    i=i+1
    j=j-1
    if [ $i -lt $y ]; then
        pdfnup $1 --nup 2x1 --pages $j,$i --outfile partwkj$i.pdf
    fi
    i=i+1
    j=j-1
done
pdftk partwkj*.pdf cat output libro.pdf
rm partwkj*.pdf

Thanks a lot,
Antonio

I tried to reconstruct your problem and solution, respectively, at first attempt coming up with:

#! /bin/bash

if [ -f $1 ]
then
  TOTAL=$( pdfinfo $1 | grep 'Pages' | awk -F ' ' '{print $2}' )
else
  echo "Usage: sh book.sh [input file]"
  exit 1
fi

IDX=1
IDX_A=1
IDX_B=$TOTAL
while [ $IDX_A -lt $IDX_B ]
do
  if [ $(( $IDX_A % 2 )) -eq 0 ]
  then
    pdfnup $1 --nup 2x1 --pages $IDX_B,$IDX_A --outfile temp.$IDX.pdf > /dev/null 2>&1
  else
    pdfnup $1 --nup 2x1 --pages $IDX_A,$IDX_B --outfile temp.$IDX.pdf > /dev/null 2>&1
  fi
  IDX_A=$(( $IDX_A + 1 ))
  IDX_B=$(( $IDX_B - 1 ))
  IDX=$(( $IDX + 1 ))
done

pdftk temp.*.pdf cat output book.pdf > /dev/null 2>&1
rm temp.*.pdf
exit 0

# [game over]

Due to a lack of appropriate test files, however, this only works for even-numbered input files (!).

Hello! I finally made the script... and thanks dr. house! you gave me the clue!

(if someone want to use it just have to put it in ~/.gnome2/nautilus-scripts/ and give permissions of execution... it will appear in a folder called Scripts when you right click anywhere, into nautilus, of course...)

so here it is with some eventual modifications...

#!/bin/bash

## Recuerde instalar pdfjam y pdftk, es decir:
## sudo apt-get install pdftk pdfjam
##
## Nota: si su pdf no tiene contraportada, ponga ahi abajo un 0.


TOTAL=$( pdftk "$1" dump_data output | grep -i Num | cut -d \  -f2 )
i=1
j=$TOTAL
k=1
l=""
NOMBREFICH=${1##*/}
NOMBREFICH=${NOMBREFICH%%.pdf}
PARTES=""
CONTRAPORTADA=1


while [ -e .partwkj"$k"_*.pdf ]; do
	k=$(( $k + 1 ))
done
if [ -e "$NOMBREFICH"_libro"$l".pdf ]; then
	l=1
fi
while [ -e "$NOMBREFICH"_libro"$l".pdf ]; do
	l=$(( $l + 1 ))
done

if [ $(( $TOTAL % 2 )) -eq 0 ]; then
	pdfnup "$1" --nup 2x1 --pages $j,$i --outfile .partwkj"$k"_$i.pdf
	PARTES=".partwkj"$k"_$i.pdf"
	i=$(( $i + 1 ))
	j=$(( $j - 1 ))
	if [ $i -lt $(((($TOTAL + 1) / 2) + 1)) ]; then
		pdfnup "$1" --nup 2x1 --pages $i,$j --outfile .partwkj"$k"_$i.pdf
		PARTES="$PARTES .partwkj"$k"_$i.pdf"
		i=$(( $i + 1 ))
		j=$(( $j - 1 ))
	fi
else
	if [ $CONTRAPORTADA -eq 1 ]; then
		pdfnup "$1" --nup 2x1 --pages $j,$i --outfile .partwkj"$k"_$i.pdf
		PARTES=".partwkj"$k"_$i.pdf"
		i=$(( $i + 1 ))
		j=""
		if [ $i -lt $(((($TOTAL + 1) / 2) + 1)) ]; then
			pdfnup "$1" --nup 2x1 --pages $i,$j --outfile .partwkj"$k"_$i.pdf
			PARTES="$PARTES .partwkj"$k"_$i.pdf"
			i=$(( $i + 1 ))
			j=$(( $TOTAL - 1 ))
		fi
	else
		j=""
		pdfnup "$1" --nup 2x1 --pages $j,$i --outfile .partwkj"$k"_$i.pdf
		PARTES=".partwkj"$k"_$i.pdf"
		i=$(( $i + 1 ))
		j=$TOTAL
		if [ $i -lt $(((($TOTAL + 1) / 2) + 1)) ]; then
			pdfnup "$1" --nup 2x1 --pages $i,$j --outfile .partwkj"$k"_$i.pdf
			PARTES="$PARTES .partwkj"$k"_$i.pdf"
			i=$(( $i + 1 ))
			j=$(( $j - 1 ))
		fi
	fi
fi

while [ $i -lt $(((($TOTAL + 1) / 2) + 1)) ]; do
	pdfnup "$1" --nup 2x1 --pages $j,$i --outfile .partwkj"$k"_$i.pdf
	PARTES="$PARTES .partwkj"$k"_$i.pdf"
	i=$(( $i + 1 ))
	j=$(( $j - 1 ))
	if [ $i -lt $(((($TOTAL + 1) / 2) + 1)) ]; then
		pdfnup "$1" --nup 2x1 --pages $i,$j --outfile .partwkj"$k"_$i.pdf
		PARTES="$PARTES .partwkj"$k"_$i.pdf"
	fi
	i=$(( $i + 1 ))
	j=$(( $j - 1 ))
done
pdftk $PARTES cat output "$NOMBREFICH"_libro"$l".pdf
rm .partwkj"$k"_*.pdf

Thanks,
Antonio

1 Like