ProgettoMartinelli2023/Fig_src/gnuplot/bin/pdf2pdfcrop.bash

79 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
# It crops and converts the pdf file into pdf file,
# HIRES="%%HiResBoundingBox"
# HIRES="%%BoundingBox"
E_BADARGS=1
while getopts ":rh" opt; do
case $opt in
r)
if [ "${OPTARG:0:1}" = "-" ]; then
echo "Invalid parameter \"${OPTARG}\" provided for agurment \"-${opt}!\""
hflag=1
else
# echo "-n was triggered! Parameter: $OPTARG" >&2
rflag=1
fi
;;
h)
if [ "${OPTARG:0:1}" = "-" ]; then
echo "Invalid parameter \"${OPTARG}\" provided for agurment \"-${opt}!\""
hflag=1
else
# echo "-h was triggered! Parameter: $OPTARG" >&2
hflag=1
fi
;;
\?)
echo "Invalid option: -$OPTARG" >&2
hflag=1
E_BADARGS=65
;;
:)
echo "Option -$OPTARG requires an argument." >&2
hflag=1
E_BADARGS=65
;;
esac
done
shift $(($OPTIND - 1))
function help {
echo -e "Usage: \t$0 [-r] <basefilename.pdf>"
echo -e "This script generates the zero-margin cropped version of the input pdf file with suffix -crop."
echo -e "Option -r: if this option is specified, the input file is replaced by the cropped version."
}
if [ "$#" -lt 1 ] || [ "$hflag" ]; then
help
exit $E_BADARGS
fi
EXT=$(echo "${1##*.}" | awk '{print tolower($0)}')
if [ "${EXT}" != "pdf" ]; then
echo "Ops! No pdf file!"
help
exit $E_BADARGS
fi
basename=$(basename ${1} .pdf)
dirname=$(dirname ${1})
echo "Cropping $1 ..."
if [ "$rflag" == "1" ]; then
pdfcrop -hires "${dirname}/${basename}.pdf" "${dirname}/${basename}-crop.pdf"
echo "${dirname}/${basename}-crop.pdf -> ${dirname}/${basename}.pdf"
mv -f "${dirname}/${basename}-crop.pdf" "${dirname}/${basename}.pdf"
else
pdfcrop -hires "${dirname}/${basename}.pdf" "${dirname}/${basename}-crop.pdf"
fi
# BBOX=$(gs -dSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox $(basename ${a} .pdf)".pdf" 2>&1 | awk '$1 ~ /'${HIRES}'/ {printf "%s %s %s %s", $2, $3, $4, $5}' )
# gs -sOutputFile=$(basename ${a} .pdf)"-crop.pdf" \
# -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 \
# -dNOPAUSE -dBATCH \
# -c "[/CropBox [${BBOX}] /PAGES pdfmark" \
# -f $(basename ${a} .pdf)".pdf"