Skip to content

Instantly share code, notes, and snippets.

@ressy
Created October 25, 2016 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ressy/f8cad426d22b64c28181051058293108 to your computer and use it in GitHub Desktop.
Save ressy/f8cad426d22b64c28181051058293108 to your computer and use it in GitHub Desktop.
Slice a long image into a PDF of letter-sized pages
#!/usr/bin/env bash
#
# Slice a long image into a PDF of letter-sized pages.
# Requires ImageMagick.
# Page size
H="11"
W="8.5"
# Input handling
image="$1"
if [ ! $image ]; then echo "usage: $0 image-file"; exit 1; fi
name="$(echo $image | cut -f 1 -d '.')"
# Caluclate dimensions
dims=$(identify -format "%w %h" "$image")
width=$(echo "$dims" | cut -f 1 -d ' ')
height=$(echo "$dims" | cut -f 2 -d ' ')
pages=$(bc <<< "scale=3; ($height/$width)/($H/$W)")
pages_N=$(bc <<< "($pages+0.5)/1-1") # round to integer (sorta), then 1
pageheight=$(bc <<< "$height/$pages")
echo "Pixels Total: ${width}x${height}"
echo "Pages Total: $pages"
echo "Page height in pixels: $pageheight"
# Slice main image into pages
for x in $(seq 0 $pages_N)
do
top=$(bc <<< "${pageheight}*${x}")
convert "$image" -crop "${width}x${pageheight}+0+$top" "$name-$(printf %.2d $x).jpg"
done
# Merge separate pages into single PDF
convert ${name}-*.jpg ${name}.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment