2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00

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.
This commit is contained in:
Tigran Aivazian 2012-10-24 14:28:43 +01:00
parent 0feb47335f
commit 1264adcda1

76
utils/pdfattach Executable file
View File

@ -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