Created
October 10, 2021 19:20
-
-
Save ebraminio/ede132671ca475c88694b04bfbafd9b3 to your computer and use it in GitHub Desktop.
Split each page of a PDF document in two using 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
#!/usr/bin/env zx | |
await $`gs -q -dNOPAUSE -dBATCH -o ${'out_' + argv.input} -sDEVICE=pdfwrite ${ | |
(await $`pdftk ${argv.input} dump_data`).stdout | |
.split('\n') | |
.filter(line => line.startsWith('PageMediaDimensions: ')) | |
.map(line => line.split(' ').slice(1).map(x => +x.replace(/,/g, ''))) | |
.reduce((acc, page, i) => { | |
const w = Math.floor(page[0] / 2); | |
const h = Math.floor(page[1]); | |
return acc.concat([ | |
`-g${w * 10}x${h * 10}`, '-c', `<</PageOffset [-${w} 0]>> setpagedevice`, | |
`-dFirstPage=${i + 1}`, `-dLastPage=${i + 1}`, '-f', argv.input, | |
`-g${w * 10}x${h * 10}`, '-c', `<</PageOffset [0 0]>> setpagedevice`, | |
`-dFirstPage=${i + 1}`, `-dLastPage=${i + 1}`, '-f', argv.input | |
]); | |
}, []) | |
}` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment