My PDF attach_files script
pdftk is a awesome toolkit to modify PDF files. The shell script below
converts a input file to a PDF file with the help of Open- or LibreOffice and
attaches the source to the end of the output file. It does an additional
convertion if the input file is a docx
, rft
or txt
file.
#!/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}