#!/bin/bash #Copyright (C) 2008 Richard G. Heck, Jr. #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. #You should have received a copy of the GNU General Public License #along with this program; if not, write to the Free Software #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA #02110-1301, USA. #Change log #Version 0.1: First public release PROG=`basename $0`; function printUsage { cat </dev/null 2>/dev/null; then echo "This script needs the program $prog. It does not seem to be in your path."; exit 1; fi done # End prereq check ############################################################################ #Check if the remaining argument is a single argument... if [ "$#" -ne "1" ]; then printUsage; exit 2; fi PDFFILE="$@"; BASEFILE=$(basename "$PDFFILE"); CURDIR=$(pwd); TEMPDIR="/tmp/$PROG-$$"; IMGBASE="$TEMPDIR/$BASEFILE"; # base name for extracted images OPTS="-f $FIRSTPG"; if [ -n "$LASTPG" ]; then OPTS="$OPTS -l $LASTPG"; fi if [ -z "$OUTFILE" ]; then OUTFILE=${BASEFILE%.pdf}; fi OUTFILE="$CURDIR/$OUTFILE"; mkdir $TEMPDIR || exit 1; if [ -n "$VERBOSE" ]; then echo "Extracting pages from PDF..."; fi $DEBUG $PDFIMGS $OPTS $PDFFILE $IMGBASE; pushd $TEMPDIR >/dev/null || exit 1; # We need to check whether the extraction got the page numbers wrong, # which it does in certain cases. if [ "$PDFIMGPRE" = "pdftoppm" ]; then if ls $BASEFILE-?.pbm >/dev/null 2>&1; then for fil in $BASEFILE-?.pbm; do mv $fil $BASEFILE-0${fil#$BASEFILE-}; done fi fi PAGES=$(ls -1 $BASEFILE*.pbm 2>/dev/null); if [ -z "$PAGES" ]; then echo "No pages appear to have been extracted from the pdf. Aborting."; exit 1; fi if [ -n "$VERBOSE" ]; then echo "PDF Pages: "; echo $PAGES; fi #If cropping has been requested, we need to parse the argument. if [ -n "$CROP" ]; then WH=${CROP%%+*}; WIDTH=${WH%x*}; HEIGHT=${WH#*x}; OFFS=${CROP#*+}; XOFF=${OFFS%+*}; YOFF=${OFFS#*+}; if [ -n "$VERBOSE" ]; then echo "Cropping: W=$WIDTH H=$HEIGHT X=$XOFF Y=$YOFF"; fi; fi if [ -n "$CROPEVEN" ]; then WHEVEN=${CROPEVEN%%+*}; WIDTHEVEN=${WHEVEN%x*}; HEIGHTEVEN=${WHEVEN#*x}; OFFSEVEN=${CROPEVEN#*+}; XOFFEVEN=${OFFSEVEN%+*}; YOFFEVEN=${OFFSEVEN#*+}; if [ -n "$VERBOSE" ]; then echo "Cropping even: W=$WIDTHEVEN H=$HEIGHTEVEN X=$XOFFEVEN Y=$YOFFEVEN"; fi; fi # this tests whether cropping or some other sort of conversion has been requested if [ -n "$CROP$CONVERT" ]; then ODD="TRUE"; for FIL in $PAGES; do OLDFIL="old-$FIL"; mv $FIL $OLDFIL || exit 1; if [ -n "$CROPEVEN" ]; then if [ -n "$ODD" ]; then #odd page W=$WIDTH; H=$HEIGHT; X=$XOFF; Y=$YOFF; ODD=""; else W=$WIDTHEVEN; H=$HEIGHTEVEN; X=$XOFFEVEN; Y=$YOFFEVEN; ODD="TRUE"; fi elif [ -n "$CROP" ]; then W=$WIDTH; H=$HEIGHT; X=$XOFF; Y=$YOFF; fi OPTS=""; if [ -n "$CROP" ]; then OPTS="-page ${W}x${H}-${X}-${Y} -crop ${W}x${H}+0+0"; fi OPTS="$OPTS $CONVERT"; if [ -n "$VERBOSE" ]; then echo "Converting $OLDFIL to $FIL"; fi $DEBUG convert $OPTS $OLDFIL $FIL; done #for fi # Are we doing 2 in 1? if [ -n "$TWOPAGE" ]; then PAGE=""; NUM=0; NEWPAGES=""; for FIL in $PAGES; do # If we don't yet have a first page, this one is it. if [ "$PAGE" = "" ]; then PAGE="$FIL"; # Otherwise we have the first page, so... else NUM=$(($NUM + 1)); #just a counter to form filenames # normalize it if (($NUM < 10)); then P="0$NUM"; else P=$NUM; fi; NEWPAGE="page-$$-$P.pbm"; if [ -n "$VERBOSE" ]; then echo "Creating page $NEWPAGE from $PAGE and $FIL"; fi $DEBUG montage -geometry +0+0 $PAGE $FIL $NEWPAGE; #No first page any more. PAGE=""; NEWPAGES="$NEWPAGES $NEWPAGE"; fi; done #If there were an odd number of pages, then one page will be left over, so... if [ -n "$PAGE" ]; then #...we use ImageMagick to create an empty page... EMPTYPAGE="$TEMPDIR/emptyPage-$$.pbm"; #...of the same size as the other pages... SIZE=`identify -format '%wx%h' $PAGE`; NUM=$(($NUM + 1)); if (($NUM < 10)); then P="0$NUM"; else P=$NUM; fi; if [ -n "$VERBOSE" ]; then echo "Creating empty page $EMPTYPAGE at $SIZE"; fi; $DEBUG convert -size $SIZE xc:white $EMPTYPAGE; NEWPAGE="page-$$-$P.pbm"; if [ -n "$VERBOSE" ]; then echo "Creating page $NEWPAGE from $PAGE and $EMPTYPAGE"; fi; #...and collate the last page with the empty page. $DEBUG montage -geometry +0+0 $PAGE $EMPTYPAGE $NEWPAGE; NEWPAGES="$NEWPAGES $NEWPAGE"; fi # so now we're working with these... PAGES="$NEWPAGES"; fi # convert to djvu DJVUS=""; for FIL in $PAGES; do NEWFIL="$FIL.djvu"; if [ -n "$VERBOSE" ]; then echo "Converting $FIL to $NEWFIL"; fi cjb2 $FIL $NEWFIL; DJVUS="$DJVUS $NEWFIL"; done if [ -n "$VERBOSE" ]; then echo "Creating $OUTFILE.djvu"; fi $DEBUG djvm -c $OUTFILE.djvu $DJVUS; popd >/dev/null || exit 1; #And we remove any temporary files we created, unless asked not to do so. if [ -z "$DEBUG" ] && [ -z "$KEEPFILES" ]; then rm -Rf $TEMPDIR; fi