Script for converting a pdf to book format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script for converting a pdf to book format
# 1  
Old 10-28-2009
Bug 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:

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
# 2  
Old 10-29-2009
I tried to reconstruct your problem and solution, respectively, at first attempt coming up with:

Code:
#! /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 (!).
# 3  
Old 10-29-2009
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...
Code:
#!/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

Last edited by dokan; 11-12-2009 at 06:45 PM.. Reason: Updating Script
This User Gave Thanks to dokan For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting secured pdf files to pdf using acroread

Does anybody have idea of Converting secured pdf files to pdf using acroread ? ---------- Post updated at 04:49 PM ---------- Previous update was at 04:44 PM ---------- This file is not password protected. (4 Replies)
Discussion started by: Soham
4 Replies

2. Shell Programming and Scripting

Converting text files to xls through awk script for specific data format

Dear Friends, I am in urgent need for awk/sed/sh script for converting a specific data format (.txt) to .xls. The input is as follows: >gi|1234|ref| Query = 1 - 65, Target = 1677 - 1733 Score = 8.38, E = 0.6529, P = 0.0001513, GC = 46 fd sdfsdfsdfsdf fsdfdsfdfdfdfdfdf... (6 Replies)
Discussion started by: Amit1
6 Replies

3. UNIX for Dummies Questions & Answers

Ghostscript error while converting PDF to Postscript

Hi, I have been using Ghostscript to convert PDF to Postscript (PS) file. GS version : 8.15.2. GS command used is : gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pswrite -sOutputFile=output1.ps input1.pdf File format conversion to PS is success in majority of the cases but some PDF files are... (0 Replies)
Discussion started by: geeveejey
0 Replies

4. Shell Programming and Scripting

Converting windows format file to unix format using script

Hi, I am having couple of files which i used to copy from windows to Linux, so now in case of text files (CTRL^M) appears at end of line. I know i can convert this windows format file to unix format file by running dos2unix. My requirement here is that i want to do it automatically using a... (5 Replies)
Discussion started by: sarbjit
5 Replies

5. Shell Programming and Scripting

Script for converting a pdf to book format: an update

With reference to , the script posted by dokan fails via a Bash syntax error because my version of pdftk 1.41 has multiple lines matching to "Num", which are stored in the TOTAL variable. If we modify the first non-ignored line of the script from # Original TOTAL=$( pdftk "$1" dump_data output |... (1 Reply)
Discussion started by: aldebrn
1 Replies

6. Shell Programming and Scripting

Converting html to pdf perl

Hi All, I have a requirement of converting an html form into pdf using perl. The html form contains images, tables and css implementation. I tried using various perl modules but failed to achive the target. I succeeded in generating a pdf from the html file using... (2 Replies)
Discussion started by: DILEEP410
2 Replies

7. UNIX for Dummies Questions & Answers

Converting multiple latex files to pdf

Hi everybody! This is my first post. I am a student and I'm familiarizing myself with linux. I have lots of directories each containing file called "zadaci.latex". I want to convert them to pdf's and rename them to <directory_name>.pdf. I read alot on this forum and came up with this script:... (2 Replies)
Discussion started by: Element9
2 Replies

8. AIX

AIX converting PDF to PS

my app creates pdf and prints them on windows. I want to run the same app on AIX 5.2. I convert PDF to PS using acroread command. But some options of acroread like landscape etc do not work. I came to know from google that there is bug with acroread for AIX with landscape option. Can you suggest... (3 Replies)
Discussion started by: vinayakshukre
3 Replies

9. Shell Programming and Scripting

Converting ps file to pdf

I have a number of post script files (*.ps) which I need to convert into Pdf. I am using the ps2pdf command for this, but the trouble is that this commands takes only one input at a time. So the command ps2pdf *.ps doesnt work. how can I make a script which will take each .ps file from... (3 Replies)
Discussion started by: jasjot31
3 Replies

10. UNIX for Advanced & Expert Users

converting PDF to text, rtf doc format

Hi all Is there any program which can convert PDF to word processor file ? If the PDF has smart quotes, bullet icons, copyright and trademark symbols, etc. what happens to them intext format? So ideally would like to conver into rtf or doc. Thanks SS (1 Reply)
Discussion started by: saurya_s
1 Replies
Login or Register to Ask a Question