Skip to content

Instantly share code, notes, and snippets.

@ebraminio
Created October 10, 2021 19:20
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 ebraminio/ede132671ca475c88694b04bfbafd9b3 to your computer and use it in GitHub Desktop.
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
#!/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