Utility for attaching files inside PDF

This utility creates a PDF file containing specified files embedded into
it as attachments. The attachments can be extracted from the PDF file by
using extr utility.
I placed this in a separate "utils" directory to signify that this is
NOT intended to run on a Kindle (it requires pdfLaTeX to generate the
PDF file) but only on the desktop Linux workstation.
pull/2/merge
Tigran Aivazian 12 years ago committed by Qingping Hou
parent d90b01dbfd
commit 622e2959b3

@ -0,0 +1,76 @@
#!/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)
function usage()
{
echo "Usage: $progname -o file.pdf -i file1.djvu [-i file2.mp3] ..."
exit 1
}
if (! getopts ":o:i:" opt); then
echo "$progname: Missing options." >&2
usage
fi
declare outfile=""
declare -a infiles=()
declare -i infcount=0 outfcount=0
while getopts ":i:o:" opt; do
case $opt in
o)
outfile="$OPTARG"
outbase=$(basename $outfile | cut -d'.' -f1)
targetdir=$(dirname $outfile)
((outfcount++))
;;
i)
infiles[$infcount]=$(readlink -f "$OPTARG")
((infcount++))
;;
\?)
echo "$progname: Invalid option: -$OPTARG" >&2
usage
;;
:)
echo "$progname: Option -$OPTARG requires an argument." >&2
usage
;;
esac
done
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
> ${outbase}.tex
echo -E "\documentclass{book}" >> ${outbase}.tex
echo -E "\usepackage{attachfile}" >> ${outbase}.tex
echo -E "\begin{document}" >> ${outbase}.tex
echo -E "\pagestyle{empty}" >> ${outbase}.tex
for ((i = 0 ; i < ${#infiles[*]} ; i++));
do
echo "\attachfile{${infiles[$i]}}" >> ${outbase}.tex
done
echo -E "\end{document}" >> ${outbase}.tex
pdflatex -halt-on-error ${outbase} > /dev/null && mv ${outbase}.pdf $targetdir
cd - > /dev/null
rm -rf $workdir
Loading…
Cancel
Save