Last active
August 29, 2015 14:01
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /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