Skip to content

Instantly share code, notes, and snippets.

@islander
Created April 11, 2015 02:51
Show Gist options
  • Select an option

  • Save islander/096171b87f4016c8c011 to your computer and use it in GitHub Desktop.

Select an option

Save islander/096171b87f4016c8c011 to your computer and use it in GitHub Desktop.
#!/bin/sh
# http://christoph-polcin.com/
TMP=/tmp
[ $# -lt 1 ] && \
echo "usage: $(basename $0) input_file [output.pdf]" && \
exit 1
[ ! -f "$1" ] && \
echo "input file not found: $1" && \
exit 1
IN=$1
NAME=$(echo -n "$IN" | sed 's/\.[^\.]*$//')
if [ $# -gt 1 ]; then
OUT=$2
else
OUT=./${NAME}.pdf
fi
[ -e "$OUT" ] && \
echo "output already exists: $OUT" && \
exit 1
if which libreoffice >/dev/null; then
CONVERTER=libreoffice
else
CONVERTER=openoffice
fi
EXT=$(echo -n "$IN" | sed 's/^.*\.//;s/.*/\L&/')
case "$EXT" in
pdf)
echo "inputfile is already a pdf: $IN"
exit 1
;;
docx|rtf|txt)
$CONVERTER --convert-to odt --headless --outdir ${TMP} "$IN"
IN=${TMP}/${NAME}.odt
;;
*)
;;
esac;
$CONVERTER --convert-to pdf --headless --outdir ${TMP} "$IN"
pdftk "${TMP}/${NAME}.pdf" \
attach_files "$IN" topage end \
output "$OUT"
rm -f "${TMP}/${NAME}.pdf"
echo output: ${OUT}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment