#!/bin/bash # # Syntax: dvi2epspdf [-h] [-f] [-a] [-m method] [-k] file[.dvi] [...] # # Purpose: Transforms a single-page dvi file into an eps or pdf file with a # tight bounding box (useful for figures if you have used psfrag). # If the -f flag is specified only the pdf file is created. # If the -a flag is specified both eps and pdf files are created. # Use the -t flag to specify the temporaty directory in which # all intermediate files should be created and stored. # If the -k flag is specified, all intermediate files are kept. # The -m flag determines the method use to create an eps file # 1 : dvips + ps2epsi # 2 : dvips + ps2epsi + eps2eps # 3 : dvips -E + eps2eps # 4 : dvips + ps2epsi + epstodf + pdftops # 5 : dvips (fallback method, last resort) # The default method is method 1 (if that one fails, it is recommended # to try method 4 next and/or to use the script adjust_eps_bbox; the # other methods may result in bitmapping of some fonts). # The pdf file is created using epstopdf unless a p is append to # method; if p is appended, then ps2pdf is used. # The -h flag shows the help info for this command. # # See also: replace_psfrag, adjust_eps_bbox # # Note: This program is inspired by pdfrack # (http://www.enseeiht.fr/~boyer/Pdfrack/) # Note: There are also two hidden options (used by remove_psfrag, viz. # -d destination_dir and -t temporary_dir). # Created: Aug 7, 2004 by Bart De Schutter # Last revised: Dec 23, 2015 by Bart De Schutter # # See http://www.deschutter.info/util/scripts.html for the latest version of # this script. # Status: public # Category: latex # # Initialize options # pdf_flag=0 all_flag=0 method=1 # Default keep_files=0 use_epstopdf=1 tmp_dir="" tmp_dir_specified=0 dest_dir="." dvips_options_for_pdf="-Ppdf -G0 -j0" dvips_options_for_eps="" # ps2pdf options taken from www.paperplaza.net pdf_option="-dCompatibilityLevel=1.4 -dMAxSubsetPct=100 -dSubsetFonts=true" pdf_option="$pdf_option -dEmbedAllFonts=true" # ps2pdf option to crop eps to bounding box instead pdf_option="$pdf_option -dEPSCrop" ##-dUseMediaBox -dAutoRotatePages=/None -dAutoPositionEPSFiles=false ## -dUseCropBox=true ##-sPAPERSIZE=" # # Process options and input arguments. # while getopts :fhakm:t:d: option do case "$option" in f) pdf_flag=1;; a) all_flag=1;; k) keep_files=1;; h) sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1;; m) method=$OPTARG;; t) tmp_dir=$OPTARG tmp_dir_specified=1;; d) dest_dir=$OPTARG;; :) echo >&2 echo >&2 "Option -$OPTARG requires an argument." echo >&2 sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 echo >&2 exit 1;; \?) echo >&2 echo >&2 "Unknown option: $OPTARG" echo >&2 sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1;; esac done shift $(($OPTIND-1)) if [[ $# -eq 0 ]] then echo >&2 echo >&2 "ERROR: ${0##*/} requires at least 1 input argument." echo >&2 sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1 fi if [[ "$method" =~ ^[0-9]+p$ ]]; then use_epstopdf=0 method=${method%p} fi if [[ !( "$method" =~ ^[0-9]+$ ) || ( $method -lt 1 ) || \ ( $method -gt 5 ) ]]; then echo >&2 echo >&2 "ERROR: Wrong value for method: $method" echo >&2 " Run ${0##*/} -h for more information." echo >&2 exit 1 fi declare -a ext_list if [[ $all_flag -eq 1 ]] then ext_list=( "eps" "pdf" ) elif [[ $pdf_flag -eq 1 ]]; then ext_list="pdf" else ext_list="eps" fi # # Create temporary directory if not yet specified. # if [[ -n "$tmp_dir" ]] then if [[ ! -d "$tmp_dir" ]] then echo >&2 echo >&2 "ERROR: The specified temporary directory $tmp_dir"\ "does not exist."; echo >&2 exit 1 fi else if [[ -d ~/tmp ]] then tmp=~/tmp elif [[ ( -n "$TMPDIR" ) || ( ! -d "$TMPDIR" ) ]] then tmp="$TMPDIR" elif [[ -d /tmp ]] then tmp=/tmp else tmp=`pwd` fi tmp_template="$tmp/${0##*/}__$(date '+%Y_%m_%d__%H_%M_%S')__XXXXXXXXXXXX" tmp_dir=$(mktemp -d "$tmp_template") if [[ $? -ne 0 ]] then echo >&2 echo >&2 "ERROR: Cannot create temporary directory using template" echo >&2 " $tmp_template" echo >&2 exit 1 fi fi # # Process files. # for fname in "$@" do base_name=${fname%.dvi} dvi_file=$base_name.dvi eps_file=${dest_dir}/$base_name.eps pdf_file=${dest_dir}/$base_name.pdf echo echo "Transforming $dvi_file" # echo for ext in "${ext_list[@]}" do eval dvips_options="\$dvips_options_for_$ext" tmp_base="$tmp_dir/${base_name}_for_$ext" case "$method" in 1 ) # # dvips + ps2epsi # tmp_file_1=${tmp_base}_1.ps tmp_file_eps=${tmp_base}_eps.eps \dvips $dvips_options -o "$tmp_file_1" "$dvi_file" ps2epsi "$tmp_file_1" "$tmp_file_eps" ;; 2 ) # # dvips + ps2epsi + eps2eps # tmp_file_1=${tmp_base}_1.ps tmp_file_2=${tmp_base}_2.eps tmp_file_eps=${tmp_base}.eps \dvips $dvips_options -o "$tmp_file_1" "$dvi_file" ps2epsi "$tmp_file_1" "$tmp_file_2" eps2eps "$tmp_file_2" "$tmp_file_eps" ;; 3 ) # # dvips -E + eps2eps # tmp_file_1=${tmp_base}_1.ps tmp_file_2=${tmp_base}_2.eps tmp_file_eps=${tmp_base}.eps \dvips $dvips_options -E -o "$tmp_file_1" "$dvi_file" ps2epsi "$tmp_file_1" "$tmp_file_2" eps2eps "$tmp_file_2" "$tmp_file_eps" ;; 4 ) # # dvips + ps2epsi + epstodf + pdftops # tmp_file_1=${tmp_base}_1.ps tmp_file_2=${tmp_base}_2.eps tmp_file_3=${tmp_base}_3.pdf tmp_file_eps=${tmp_base}.eps \dvips $dvips_options -o "$tmp_file_1" "$dvi_file" ps2epsi "$tmp_file_1" "$tmp_file_2" epstopdf --outfile="$tmp_file_3" "$tmp_file_2" pdftops -eps "$tmp_file_3" "$tmp_file_eps" ;; 5 ) # # dvips (fallback method) # tmp_file_eps=${tmp_base}_eps.eps \dvips $dvips_options -o "$tmp_file_eps" "$dvi_file" ;; esac if [[ "$ext" == "pdf" ]] then if [[ $use_epstopdf -eq 1 ]] then epstopdf --outfile="$pdf_file" "$tmp_file_eps" else ps2pdf $pdf_option "$tmp_file_eps" "$pdf_file" fi else # So $ext == eps \mv "$tmp_file_eps" "$eps_file" fi # # If tmp_dir was specified and intermediate files should not be # kept, then we now already delete these intermediate files. # if [ $keep_files -eq 0 ] then \rm "${tmp_base}".ps "${tmp_base}".eps "${tmp_base}"_?.ps \ "${tmp_base}"_?.eps 2>/dev/null fi done done if [ $tmp_dir_specified -eq 0 ] then if [ $keep_files -eq 0 ] then rmdir "$tmp_dir" else echo echo "Temporary files have been stored in $tmp_dir" echo fi fi