From 622e2959b37242a2ea35bbeae9653027ad26423b Mon Sep 17 00:00:00 2001 From: Tigran Aivazian Date: Wed, 24 Oct 2012 14:28:43 +0100 Subject: [PATCH] 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. --- utils/pdfattach | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100755 utils/pdfattach diff --git a/utils/pdfattach b/utils/pdfattach new file mode 100755 index 000000000..10a729513 --- /dev/null +++ b/utils/pdfattach @@ -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 +# + +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