[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

PDF conversion script



In case anyone is interested, the attached elaps script accepts the option
"--pdf" (or "-pdf") and creates a PDF file from eepic/epix files. When the
option is set, the EPS file is deleted automatically.

--Andy

Andrew D. Hwang			ahwang at mathcs dot holycross dot edu
Department of Math and CS	http://mathcs.holycross.edu/~ahwang
College of the Holy Cross	(508) 793-2458 (Office: 320 Swords)
Worcester, MA, 01610-2395	(508) 793-3530 (fax)
#!/bin/bash
# 
# elaps: Convert an epix file to encapsulated Postscript, i.e. epix2eps
# 
# June 25, 2002, Andrew D. Hwang, ahwang@mathcs.holycross.edu
# Sept 02, 2002, Printer options to dvips added (Walter A. Kehowski)
# Sept 15, 2002, Accept all epix options (ADH)
# Sept 20, 2002, Accept eepic file as input (ADH)
# Dec  20, 2002, Accept .xp
# Apr  14, 2003, usepackage{rotating}
# June 11, 2003, PDF option
#
PROG=$(basename $0)

# default options
DVIPS_OPTS="-f"
ELAPS_PKGS="latexsym,amsmath,rotating,epic,eepic,pstcol"

ELAPS_EXTENSIONS="eepic xp cc c C cpp" # or NULL
ELAPS_DOT_EXT=".eepic|.xp|.cc|.c|.C|.cpp" # output string for "usage"

PID=$$
ELAPS_TEMP_EEPIC=$PROG-$PID.eepic
ELAPS_TEMP_LATEX=$PROG-$PID.tex
ELAPS_TEMP_DVI=$PROG-$PID.dvi
ELAPS_TEMP_PS=$PROG-$PID.ps
ELAPS_TEMP_EPSI=$PROG-$PID.epsi

declare ELAPS_FILEROOT
declare ELAPS_INFILE
declare ELAPS_EXT # Input file extension
ELAPS_FOUND="no"  # Found an input file?
ELAPS_PDF="no"    # Create PDF file?

ELAPS_OUT=/dev/null # Pipe stderr here
ELAPS_VERBOSE=
EPIX_OPTS=

function elaps_die {
    echo "$PROG: ERROR: $1"
    exit 1
}

function elaps_parse_options {
    # Skip command line options
    while [ "$1" != "${1#"-"}" ]; do 
	case "$1" in

	-V|-b|-i*|-o|-u|-x)
	    if [ "$2" != "${2#"-"}" -o "$2" = "" ]; then
		elaps_die "Compiler option \"$1\" cannot be followed by \"$2\""
	    else
		EPIX_OPTS="$EPIX_OPTS $1 $2";
		shift 2;
	    fi
	    ;;
	    
	-h|--help)
	    elaps_help;
	    ;;

	-v|--version)
	    elaps_version;
	    ;;

	-vv|--verbose)
	    ELAPS_VERBOSE=1
	    ELAPS_OUT=/dev/stdout
	    shift;
	    ;;

	-P*)
	    DVIPS_OPTS="$1 $DVIPS_OPTS";
	    shift;
	    ;;

	-p|--pkg)
	    if [ "$2" != "${2#"-"}" -o "$2" = "" ]; then
		elaps_die "Flag \"$1\" cannot be followed by \"$2\""
	    else
		ELAPS_PKGS="$ELAPS_PKGS,$2" &&
		echo "$PROG: Using LaTeX package \"$2\"" > $ELAPS_OUT
		shift 2;
	    fi
	    ;;

	-pdf|--pdf)
	    ELAPS_PDF="yes"
	    echo "$PROG: Creating PDF file" > $ELAPS_OUT
	    shift;
	    ;;

	-p*)
	    ELAPS_NEW_PKG=${1#"-p"} &&
	    ELAPS_PKGS="$ELAPS_PKGS,$ELAPS_NEW_PKG" &&
	    echo "$PROG: Using LaTeX package \"$ELAPS_NEW_PKG\"" > $ELAPS_OUT
	    shift;
	    ;;

	*)
	    EPIX_OPTS="$EPIX_OPTS $1";
	    shift;
	    ;;
	esac
    done

    # Assume next parameter is the input file
    ELAPS_INFILE=$1

    if [ $2 ]; then # More than one remaining argument is a fatal error
	elaps_die "No arguments allowed after input file ($1)"

    elif [ "$1" = "" ]; then
	elaps_die "You must specify an input file!"
    fi	

    # Otherwise, get file root 
    for EXT in $ELAPS_EXTENSIONS; do
	if [ $1 = ${1%%".$EXT"}.$EXT ]; then
	    if [ "$EXT" = "eepic" ]; then ELAPS_EXT="eepic"; fi
	    ELAPS_FILEROOT=${1%%".$EXT"} &&
	    ELAPS_FOUND="yes" && break
	fi
    done

    if [ "$ELAPS_FOUND" = "no" ]; then
        ELAPS_FILEROOT=$1
	if [ -f $ELAPS_FILEROOT.eepic ]; then ELAPS_EXT="eepic"; fi

    fi
} # elaps_parse_options()

function elaps_help {
    echo "Usage: $PROG [options] <infile>[$ELAPS_DOT_EXT]" >&1

if [ "$ELAPS_VERBOSE" != "" ]; then
cat <<EOF >&1

  Recognized options are:

    -vv, --verbose
      Show output messages. Mostly useful if this script hangs silently.

    -P<printer>
      dvips printer options, e.g. "-Pamz", may be given on the command 
      line. They are also read from ~/.dvipsrc if this file exists.

    -p, --pkg <LaTeX package>
      Use an additional LaTeX package in the temporary LaTeX file, e.g. 
      "-p floatflt". Multiple packages are specified one per "-p" flag.

    -pdf, --pdf
      Create PDF file instead of EPS

    -h, --help
      Print help message; accepts --verbose option.

    -v, --version
      Print version information; use --verbose option for license.

EOF
else
    echo "  \"$PROG --verbose -h\" for detailed help"
fi
    exit 0
} # End of elaps_help

function elaps_version {
cat <<VERSION1 >&1
$PROG is part of ePiX, Version 0.8.10

Copyright (C) 2001, 2002, 2003
Andrew D. Hwang <ahwang@mathcs.holycross.edu>
Department of Mathematics and Computer Science
College of the Holy Cross
Worcester, MA, 01610-2395, USA

VERSION1

if [ "$ELAPS_VERBOSE" != "" ]; then
cat <<VERSION2 >&1
  ePiX 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.
 
  ePiX 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 ePiX; if not, write to the Free Software Foundation, Inc.,
  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

VERSION2
else
    echo "  \"$PROG --verbose -v\" for license"
fi
    exit 0
} # End of elaps_version

## script starts here ##

if [ $# = 0 ]; then elaps_help; fi # exit

elaps_parse_options $@

# Create eepic file if necessary
if [ "$ELAPS_EXT" != "eepic" ]; then 
    epix $EPIX_OPTS $ELAPS_INFILE $ELAPS_TEMP_EEPIC 1> $ELAPS_OUT || 
	elaps_die "Can't create $ELAPS_FILEROOT.eepic"
elif [ -f $ELAPS_FILEROOT.eepic ]; then
    ELAPS_TEMP_EEPIC=$ELAPS_FILEROOT.eepic
else
    elaps_die "$ELAPS_FILEROOT.eepic not found"
fi

# Generate LaTeX file
cat <<EOF >> $ELAPS_TEMP_LATEX
\documentclass[12pt]{article}
\usepackage{$ELAPS_PKGS}
\pagestyle{empty}

\begin{document}
\begin{center}
\input{$ELAPS_TEMP_EEPIC}
\end{center}
\end{document}

EOF
#end of LaTeX file

# Create $ELAPS_TEMP_PS; dvips prints some non-error messages to stderr...
latex $ELAPS_TEMP_LATEX 1> $ELAPS_OUT
dvips $DVIPS_OPTS -o $ELAPS_TEMP_PS $ELAPS_TEMP_DVI &> $ELAPS_OUT

# Create eps file,
ps2epsi $ELAPS_TEMP_PS 1> $ELAPS_OUT

# fix title,
sed "s/$PROG-$PID/$ELAPS_FILEROOT/g" $ELAPS_TEMP_EPSI > $ELAPS_FILEROOT.eps

# create pdf if requested,
if [ "$ELAPS_PDF" = "yes" ]; then
# From Wenguang Wang, to avoid ghostscript rotation bug
    grep -v "^%%Orientation:" $ELAPS_FILEROOT.eps | \
	epstopdf --filter --outfile=$ELAPS_FILEROOT.pdf
    rm $ELAPS_FILEROOT.eps # clean up eps
fi

# and clean up
rm  $ELAPS_TEMP_LATEX $ELAPS_TEMP_DVI $ELAPS_TEMP_PS $ELAPS_TEMP_EPSI \
    $PROG-$PID.aux $PROG-$PID.log

if [ "$ELAPS_EXT" != "eepic" ]; then rm $ELAPS_TEMP_EEPIC; fi

exit 0