#!/bin/sh # Copyright (C) 2008 Adalbert Prokop, adalbert.prokop(%)web.de # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . #set -x # debug declare -i WIDTH declare -i HEIGHT declare -i RPS # rows per strip, multiple of 16 for JPEG RPS=16 DEBUG= AUTHOR= ALIGN=center VALIGN=middle CREATOR= DATE=$(date +%Y%m%d%H%M%S) KEYWORDS= PAPER=A4 PWIDTH=21.0 PHEIGHT=29.7 QUALITY=85 SUBJECT= TITLE= WORKAROUND=no function showhelp() { cat < Autor for the PDF file. -c|--creator Creator for the PDF file. -d|--date YYYYMMDDHHMMSS Date for the PDF file. Defaults to now. -k|--keywords Keywords for the PDF file. -s|--subject Subject for the PDF file. -t|--title Title for the PDF file. -p|--paper [A4|A5] Papersize. -q|--quality [0..100] JPEG compression quality. Defaults to $QUALITY. EOF } if [ $# -eq 0 ]; then showhelp exit 0 fi if [ -z "$(which python 2>/dev/null)" ]; then cat <<EOF This script needs python for some calculation. Please make sure there is a python executable. Right now none can be find. EOF fi eval set -- $(getopt -o a:c:d:hk:p:q:s:t:w -l author:,creator:,date:,help,keywords:,paper:,quality:,subject:,title:,workaround -- "$@") while [ "$1" != "--" ]; do case "$1" in -h|--help) showhelp exit 0 ;; -w|--workaround) WORKAROUND=yes shift ;; -a|--author) AUTHOR="$2" shift 2 ;; -c|--creator) CREATOR="$2" shift 2 ;; -d|--date) DATE="$2" shift 2 ;; -k|--keywords) KEYWORDS="$2" shift 2 ;; -s|--subject) SUBJECT="$2" shift 2 ;; -t|--title) TITLE="$2" shift 2 ;; -p|--paper) PAPER="$2" shift 2 ;; -q|--quality) if [ "$2" -gt 0 -a "$2" -le 100 ]; then QUALITY="$2" else echo "Bad quality settings $2" exit 1 fi shift 2 ;; esac done shift case "$PAPER" in a4|A4) PWIDTH=21.0 PHEIGHT=29.7 ;; a4|A4) PWIDTH=14.85 PHEIGHT=21.0 ;; *) echo "Unknown papersize $PAPER" exit 1 ;; esac for dir in "$@"; do dir="${dir%/}" if [ ! -d "$dir" ]; then echo "There is no directory called $dir" continue fi echo "Processing directory $dir" rm -f "$dir"/*.tif for file in "$dir"/*.pnm; do WIDTHHEIGHT="$(pamfile "$file" | sed -e 's/.*, \([0-9]\+\) by \([0-9]\+\) .*/\1 \2/g')" WIDTH=$(echo "$WIDTHHEIGHT"|cut -d" " -f 1) HEIGHT=$(echo "$WIDTHHEIGHT"|cut -d" " -f 2) if [ $(python -c "print abs($HEIGHT.0/$WIDTH.0 - 1.4142) > 0.1") == "True" ]; then echo "*** Resolution ratio of $file is NOT DIN norm! ***" fi XRES=$(python -c "print '%.2f' % ($WIDTH/$PWIDTH)") YRES=$(python -c "print '%.2f' % ($HEIGHT/$PHEIGHT)") RES=$(python -c "print($XRES if $XRES > $YRES else $YRES)") [ "$DEBUG" ] && echo "pamtotiff -rowsperstrip=$RPS -truecolor -xresolution=$RES -yresolution=$RES -resolutionunit=centimeter $file > ${file%.pnm}.tif" echo -n "Converting $file (${RES}dpcm) to TIFF format... " pamtotiff -rowsperstrip=$RPS -truecolor -xresolution=$RES -yresolution=$RES -resolutionunit=centimeter "$file" > "${file%.pnm}.tif" && echo "done." || echo "failed." done [ "$DEBUG" ] && echo "tiffcp -r $RPS $dir/*.tif $dir.tif" echo -n "Creating combined TIFF... " tiffcp -r $RPS "$dir"/*.tif "$dir.tif" && rm -f "$dir"/*.tif && echo "done." || echo "failed." [ "$DEBUG" ] && echo "tiff2pdf -o $dir.pdf -j -q $QUALITY -p $PAPER $dir.tif" echo -n "Creating PDF from combined TIFF image... " tiff2pdf -o "$dir.pdf" -j -q $QUALITY -p $PAPER -f \ ${TITLE:+-t"$TITLE"} \ ${AUTHOR:+-a"$AUTHOR"} \ ${CREATOR:+-c"$CREATOR"} \ ${SUBJECT:+-s"$SUBJECT"} \ ${KEYWORDS:+-k"$KEYWORDS"} \ ${DATE:+-e"$DATE"} \ "$dir.tif" && rm -f "$dir.tif" && echo "done." || echo "failed." if [ "$WORKAROUND" = "yes" ]; then echo "Applying workaround." sed -e 's#/ColorTransform 0#/ColorTransform 1#g' "$dir.pdf" > "$dir.pdf.tmp" mv -f "$dir.pdf.tmp" "$dir.pdf" fi done