Skip to content

Instantly share code, notes, and snippets.

@jwarby
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save jwarby/a392a0b02ddcc70c1145 to your computer and use it in GitHub Desktop.

Select an option

Save jwarby/a392a0b02ddcc70c1145 to your computer and use it in GitHub Desktop.
Split PDFs which have 2-page spreads into 1-per-page PDF - usage ./pdf-singleify input.pdf output.pdf. Requires pdfinfo, ghostscript and pdftk
#! /bin/bash
file="$1"
function getDimensions() {
local page_size=`pdfinfo "$file" | grep "Page size"`
[[ $page_size =~ ([0-9\.]+)[[:space:]x]+([0-9\.]+) ]]
let page_width="${BASH_REMATCH[1]/.*} / 2"
let page_height="${BASH_REMATCH[2]/.*}"
let offset="-$page_width"
}
function createPages() {
let local width="$page_width * 10"
let local height="$page_height * 10"
gs -o /tmp/"$1"-pages.pdf -sDEVICE=pdfwrite -g${width}x${height} \
-c "<</PageOffset [$2 0]>> setpagedevice" -f "$file"
}
getDimensions
createPages "left" 0
createPages "right" $offset
pdftk A=/tmp/left-pages.pdf B=/tmp/right-pages.pdf shuffle \
output "$2" verbose
# Delete temp files
rm /tmp/{left,right}-pages.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment