Skip to content

Instantly share code, notes, and snippets.

@cipri-tom
Created December 8, 2014 12:38
Show Gist options
  • Save cipri-tom/7d482b0f3c7df470691c to your computer and use it in GitHub Desktop.
Save cipri-tom/7d482b0f3c7df470691c to your computer and use it in GitHub Desktop.
Scaling PDFs with GhostScript under linux
#! /bin/bash
# modified from SO: http://stackoverflow.com/a/25932668/786559
input=$2
output=$(echo $2 | sed s/.pdf/_scaled.pdf/)
if [ $# -ne 2 ] ; then
echo "Bad arguments!"
exit
fi
# assume 0<=$1<=100 (no error checks!)
xx="595" #width of A4 in post script points
yy="842" #height of A4 in pps
ss=$(echo "scale=4; $1 / 2" | bc)
sx=$(echo "scale=4; ${xx}"'*'"( ${ss}/ 100 )" | bc)
sy=$(echo "scale=4; ${yy}"'*'"( ${ss}/ 100 )" | bc)
s=$(echo "scale=4; 1 - $1 / 100" | bc)
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \
-dCompatibilityLevel="1.3" -dPDFSETTINGS="/screen" \
-dSubsetFonts=true -dEmbedAllFonts=true \
-sPAPERSIZE=a4 -sOutputFile="${output}" \
-c "<</BeginPage{${s} ${s} scale ${sx} ${sy} translate}>> setpagedevice" \
-f "$input"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment