#!/bin/sh
##
##  pdfresize -- resize a PDF document
##  Copyright (c) 2018 Ralf S. Engelschall <rse@engelschall.com>
##  Licensed under MIT license.
##

#   determine parameters
if [ $# -ne 3 ]; then
    echo "pdfresize: USAGE: pdfresize <scale-factor> <input-pdf> <output-pdf>" 1>&2
    exit 1
fi
scale="$1"
input="$2"
output="$3"

#   determine input size in 72PT units
size=`pdfinfo $input | grep "Page size:" | sed -e 's;^[^0-9]*\([0-9]*\).*x \([0-9]*\).*;\1x\2;'`
input_w=`echo $size | sed -e 's;x.*$;;'`
input_h=`echo $size | sed -e 's;^.*x;;'`

#   calculate new size
output_w=`(echo "scale = 0"; echo "$input_w * $scale" ) | @l_prefix@/bin/bc | sed -e 's;\..*;;'`
output_h=`(echo "scale = 0"; echo "$input_h * $scale" ) | @l_prefix@/bin/bc | sed -e 's;\..*;;'`

#   perform resizing with the help of Ghostscript
@l_prefix@/bin/gs \
    -q \
    -dNOPAUSE \
    -dBATCH \
    -sDEVICE=pdfwrite \
    -dSAFER \
    -dCompatibilityLevel="1.5" \
    -dPDFSETTINGS="/printer" \
    -dSubsetFonts=false \
    -dEmbedAllFonts=true \
    -r72 \
    -g${output_w}x${output_h} \
    -sOutputFile=$output \
    -c "<</BeginPage{ $scale $scale scale 0 0 translate}>> setpagedevice" \
    -f $input

