mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
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).
109 lines
2.4 KiB
Bash
Executable File
109 lines
2.4 KiB
Bash
Executable File
#!/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 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] ..."
|
|
exit 1
|
|
}
|
|
|
|
if (! getopts ":o:" opt); then
|
|
echo "$progname: Missing options." >&2
|
|
usage
|
|
fi
|
|
|
|
if ! type pdflatex > /dev/null 2>&1 ; then
|
|
echo "$progname: pdfLaTeX program is required." >&2
|
|
exit 1
|
|
fi
|
|
|
|
if ! kpsewhich attachfile.sty > /dev/null 2>&1 ; then
|
|
echo "$progname: attachfile.sty package is required." >&2
|
|
exit 1
|
|
fi
|
|
|
|
declare outfile=""
|
|
declare -a infiles=()
|
|
declare -a infilesize=()
|
|
declare -i infcount=0 outfcount=0 totalsize=0
|
|
|
|
while getopts ":o:" opt; do
|
|
case $opt in
|
|
o)
|
|
outfile=$(readlink -f "$OPTARG")
|
|
((outfcount++))
|
|
;;
|
|
\?)
|
|
echo "$progname: Invalid option: -$OPTARG" >&2
|
|
usage
|
|
;;
|
|
:)
|
|
echo "$progname: Option -$OPTARG requires an argument." >&2
|
|
usage
|
|
;;
|
|
esac
|
|
done
|
|
|
|
shift $((OPTIND-1))
|
|
|
|
numargs=$#
|
|
for ((i=1 ; i <= $numargs ; i++))
|
|
do
|
|
fullname=$(readlink -f "$1")
|
|
if [ ! -f "$fullname" ] ; then
|
|
echo "$progname: file \"$fullname\" does not exist" >&2
|
|
usage
|
|
fi
|
|
infiles[$infcount]="$fullname"
|
|
infilesize[$infcount]=$(stat --print="%s" "$fullname")
|
|
((totalsize=totalsize+${infilesize[$infcount]}))
|
|
((infcount++))
|
|
shift
|
|
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
|
|
> 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}\small" >> tmp.tex
|
|
for ((i = 0 ; i < ${#infiles[*]} ; i++));
|
|
do
|
|
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
|
|
rm -rf $workdir
|