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

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).
This commit is contained in:
Tigran Aivazian 2012-10-24 17:15:50 +01:00
parent be1996d0a0
commit 9ad3577be8

View File

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