2012-10-24 13:28:43 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
#
|
|
|
|
# pdfattach --- embed specified file(s) in a specified PDF file
|
|
|
|
# Requires pdfLaTeX and attachfile.sty package to run
|
|
|
|
# Returns 0 on success or >0 on error
|
|
|
|
#
|
|
|
|
# Written by Tigran Aivazian <tigran@bibles.org.uk>
|
|
|
|
#
|
|
|
|
|
|
|
|
progname=$(basename $0)
|
|
|
|
|
2012-10-24 16:15:50 +00:00
|
|
|
function escape_tex_specialchars()
|
|
|
|
{
|
|
|
|
local txt=$1
|
2012-10-24 21:03:35 +00:00
|
|
|
local res=$(echo "$txt" | sed -e "s%_%\\\_%g" -e "s%&%\\\&%g")
|
2012-10-24 16:15:50 +00:00
|
|
|
echo "$res"
|
|
|
|
}
|
|
|
|
|
2012-10-24 13:28:43 +00:00
|
|
|
function usage()
|
|
|
|
{
|
2012-11-03 08:30:04 +00:00
|
|
|
echo "Usage: $progname -o file.pdf [-a author] file1.djvu [file2.mp3] ..."
|
2012-10-24 13:28:43 +00:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2012-10-24 14:57:42 +00:00
|
|
|
if (! getopts ":o:" opt); then
|
2012-10-24 13:28:43 +00:00
|
|
|
echo "$progname: Missing options." >&2
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
2012-10-24 14:57:42 +00:00
|
|
|
if ! type pdflatex > /dev/null 2>&1 ; then
|
|
|
|
echo "$progname: pdfLaTeX program is required." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! kpsewhich attachfile.sty > /dev/null 2>&1 ; then
|
|
|
|
echo "$progname: attachfile.sty package is required." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2012-10-24 13:28:43 +00:00
|
|
|
declare outfile=""
|
|
|
|
declare -a infiles=()
|
2012-10-24 19:35:10 +00:00
|
|
|
declare -a infiles_texclean=()
|
2012-10-24 16:15:50 +00:00
|
|
|
declare -a infilesize=()
|
|
|
|
declare -i infcount=0 outfcount=0 totalsize=0
|
2012-11-03 08:30:04 +00:00
|
|
|
declare author=""
|
2012-10-24 13:28:43 +00:00
|
|
|
|
2012-11-03 08:30:04 +00:00
|
|
|
while getopts ":o:a:" opt; do
|
2012-10-24 13:28:43 +00:00
|
|
|
case $opt in
|
2012-11-03 08:30:04 +00:00
|
|
|
a)
|
|
|
|
author="$OPTARG"
|
|
|
|
;;
|
2012-10-24 13:28:43 +00:00
|
|
|
o)
|
2012-10-24 14:57:42 +00:00
|
|
|
outfile=$(readlink -f "$OPTARG")
|
2012-10-24 13:28:43 +00:00
|
|
|
((outfcount++))
|
|
|
|
;;
|
|
|
|
\?)
|
|
|
|
echo "$progname: Invalid option: -$OPTARG" >&2
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
:)
|
|
|
|
echo "$progname: Option -$OPTARG requires an argument." >&2
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2012-10-24 14:57:42 +00:00
|
|
|
shift $((OPTIND-1))
|
|
|
|
|
|
|
|
numargs=$#
|
|
|
|
for ((i=1 ; i <= $numargs ; i++))
|
|
|
|
do
|
|
|
|
fullname=$(readlink -f "$1")
|
2012-10-24 22:22:21 +00:00
|
|
|
if [ ! -r "$fullname" ] ; then
|
2012-10-24 22:24:54 +00:00
|
|
|
echo "$progname: cannot access the file \"$fullname\"" >&2
|
2012-10-24 14:57:42 +00:00
|
|
|
usage
|
|
|
|
fi
|
|
|
|
infiles[$infcount]="$fullname"
|
2012-10-24 19:35:10 +00:00
|
|
|
infiles_texclean[$infcount]=$(escape_tex_specialchars $(basename "${infiles[$infcount]}"))
|
2012-10-24 16:15:50 +00:00
|
|
|
infilesize[$infcount]=$(stat --print="%s" "$fullname")
|
|
|
|
((totalsize=totalsize+${infilesize[$infcount]}))
|
2012-10-24 14:57:42 +00:00
|
|
|
((infcount++))
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
2012-10-24 13:28:43 +00:00
|
|
|
if ((infcount == 0)) ; then
|
|
|
|
echo "$progname: No input file(s) specified." >&2
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ((outfcount != 1)) ; then
|
|
|
|
echo "$progname: One (and only one) output file must be specified." >&2
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
workdir=$(mktemp --tmpdir -d pdfattach.XXXXXX)
|
|
|
|
cd $workdir
|
2012-10-24 14:57:42 +00:00
|
|
|
> tmp.tex
|
2012-10-24 19:35:10 +00:00
|
|
|
# emit TeX preamble
|
2012-10-24 14:57:42 +00:00
|
|
|
echo -E "\documentclass{book}" >> tmp.tex
|
2012-10-24 16:15:50 +00:00
|
|
|
echo -E "\usepackage[margin={1mm},papersize={9cm,12cm}]{geometry}" >> tmp.tex
|
2012-10-24 19:35:10 +00:00
|
|
|
echo -E "\usepackage{hyperref,attachfile}" >> tmp.tex
|
2012-11-03 08:30:04 +00:00
|
|
|
if [ ! -z "$author" ] ; then
|
|
|
|
echo -E "\AtBeginDocument{\hypersetup{pdfauthor={$author}}}" >> tmp.tex
|
|
|
|
fi
|
2012-10-24 14:57:42 +00:00
|
|
|
echo -E "\begin{document}" >> tmp.tex
|
2012-10-24 19:35:10 +00:00
|
|
|
echo -E "\tolerance=10000\pagestyle{empty}\fontsize{7}{13}\selectfont" >> tmp.tex
|
|
|
|
|
|
|
|
# emit the list of all files
|
2012-10-24 13:28:43 +00:00
|
|
|
for ((i = 0 ; i < ${#infiles[*]} ; i++));
|
|
|
|
do
|
2012-10-24 19:35:10 +00:00
|
|
|
echo -E "\noindent \hyperlink{L$i}{$((i+1))/${infcount}} \texttt{${infiles_texclean[$i]}} (${infilesize[$i]} bytes)" >> tmp.tex
|
2012-10-24 16:15:50 +00:00
|
|
|
echo >> tmp.tex
|
2012-10-24 13:28:43 +00:00
|
|
|
done
|
2012-10-24 19:35:10 +00:00
|
|
|
echo -E "\noindent Total size $totalsize bytes\newpage" >> tmp.tex
|
|
|
|
|
|
|
|
# now emit all the attachments, one per page
|
|
|
|
for ((i = 0 ; i < ${#infiles[*]} ; i++));
|
|
|
|
do
|
|
|
|
echo -E "\noindent\hypertarget{L$i}$((i+1))/${infcount}\\\\\texttt{${infiles_texclean[$i]}} (\textattachfile[color={0 0 0}]{${infiles[$i]}}{${infilesize[$i]} bytes})\newpage" >> tmp.tex
|
|
|
|
done
|
2012-10-24 14:57:42 +00:00
|
|
|
echo -E "\end{document}" >> tmp.tex
|
|
|
|
pdflatex -halt-on-error tmp.tex > /dev/null && mv tmp.pdf "$outfile"
|
2012-10-24 13:28:43 +00:00
|
|
|
cd - > /dev/null
|
|
|
|
rm -rf $workdir
|