From 6d02aaaa7b4cff45396e0b196357f45f3ecf0a77 Mon Sep 17 00:00:00 2001 From: Tigran Aivazian Date: Wed, 24 Oct 2012 17:15:50 +0100 Subject: [PATCH] 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). --- utils/pdfattach | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/utils/pdfattach b/utils/pdfattach index 6af91c98b..50dbb8dc3 100755 --- a/utils/pdfattach +++ b/utils/pdfattach @@ -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