#!/bin/bash ######################################################################### # PDF2DJVU version 0.10 -- Converts a PDF file to a DJVU file. # (Optional 2nd argument says how many degrees to use to rotate CCW) # # Copyright 2005 Jonathan Hanke # Original Author: Jonathan Hanke # Last updated by Jonathan Hanke on Dec 13, 2005 # # 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 ######################################################################### # Make the temporary directory tmp_dir="/tmp/`basename $1`" if ! [ -d $tmp_dir ]; then mkdir $tmp_dir fi # Find the number of pages in the pdf file num_of_pages=`pdfinfo $1 | grep Pages: | sed 's/Pages://; s/[ \t]*//'` echo "There are $num_of_pages pages in the PDF file." # UNUSED: # ------- # Extract the pages to 300dpi ppm files ##gs -q -r300 -dNOPAUSE -dBATCH -dSAFER -sDEVICE=ppmraw -sOutputFile=$tmp_dir/tmp%04d -- $1 ## This extracts all of the pages at once! for num in `seq -w 1 $num_of_pages`; do if [ -e $tmp_dir/$num.djvu ]; then echo " Found page " $num " -- " $(date) else echo " Generating page " $num " -- " $(date) # Extract the current page gs -q -r300 -dNOPAUSE -dBATCH -dSAFER -sDEVICE=ppmraw -dFirstPage=$num -dLastPage=$num -sOutputFile=$tmp_dir/newpage_$num -- $1 ## pdftoppm -r 300 -f $num -l $num $1 $tmp_dir/newpage_$num ## This one made huge files! ## pdftoppm -r 150 -f $num -l $num $1 $tmp_dir/newpage_$num ## USING THIS ONE! ## pdftoppm -r 100 -f $num -l $num $1 $tmp_dir/newpage_$num ## Not much difference from 100 interms of size, so we'll stay with 150! # Rotate CCW by the number of degrees in the second argument! if [ $# = 1 ]; then ## echo " No extra operations!" sleep 0 #Dummy command else ## echo " Rotating clockwise by $2 degrees!" convert -rotate $2 $tmp_dir/newpage_$num $tmp_dir/newpage_$num-rot$2.ppm rm -f $tmp_dir/newpage_$num # Needed to ensure that we only convert the rotated image! fi # Convert it to djvu format, then remove it for i in $tmp_dir/newpage_$num*; do cpaldjvu -dpi 300 $i $tmp_dir/$num.djvu rm -f $i done fi done # Assemble the multi-page djvu document djvu_filename=$(echo $1 | sed 's/\(.*\.\)pdf/\1djvu/') # This is the filename with .pdf replaced by .djvu djvm -c $djvu_filename $tmp_dir/*.djvu # Clean up rm -rf $tmp_dir