Enhance pdfattach script

1. Set the page size to 9x12cm as appropriate for Kindle 3
2. Together with each attachment print its base filename and file size
in bytes.
3. Print the total size of all attachments (so one can estimate whether
it is safe to extract the attachments or not).
pull/2/merge
Tigran Aivazian 12 years ago committed by Qingping Hou
parent a86e2ece23
commit 6d02aaaa7b

@ -10,6 +10,13 @@
progname=$(basename $0)
function escape_tex_specialchars()
{
local txt=$1
local res=$(echo "$txt" | sed -e "s%_%\\\_%g")
echo "$res"
}
function usage()
{
echo "Usage: $progname -o file.pdf file1.djvu [file2.mp3] ..."
@ -33,7 +40,8 @@ fi
declare outfile=""
declare -a infiles=()
declare -i infcount=0 outfcount=0
declare -a infilesize=()
declare -i infcount=0 outfcount=0 totalsize=0
while getopts ":o:" opt; do
case $opt in
@ -63,6 +71,8 @@ do
usage
fi
infiles[$infcount]="$fullname"
infilesize[$infcount]=$(stat --print="%s" "$fullname")
((totalsize=totalsize+${infilesize[$infcount]}))
((infcount++))
shift
done
@ -81,13 +91,17 @@ workdir=$(mktemp --tmpdir -d pdfattach.XXXXXX)
cd $workdir
> tmp.tex
echo -E "\documentclass{book}" >> tmp.tex
echo -E "\usepackage[margin={1mm},papersize={9cm,12cm}]{geometry}" >> tmp.tex
echo -E "\usepackage{attachfile}" >> tmp.tex
echo -E "\begin{document}" >> tmp.tex
echo -E "\pagestyle{empty}" >> tmp.tex
echo -E "\pagestyle{empty}\small" >> tmp.tex
for ((i = 0 ; i < ${#infiles[*]} ; i++));
do
echo "\attachfile{${infiles[$i]}}" >> tmp.tex
descr=$(escape_tex_specialchars $(basename "${infiles[$i]}"))
echo -E "\noindent $((i+1)): $descr (\textattachfile[color={0 0 0}]{${infiles[$i]}}{${infilesize[$i]} bytes})" >> tmp.tex
echo >> tmp.tex
done
echo -E "\noindent Total size $totalsize bytes" >> tmp.tex
echo -E "\end{document}" >> tmp.tex
pdflatex -halt-on-error tmp.tex > /dev/null && mv tmp.pdf "$outfile"
cd - > /dev/null

Loading…
Cancel
Save