-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimposition.ts
47 lines (38 loc) · 1.03 KB
/
imposition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import childProcess from 'child_process'
const calcImpotision = (pagesLength: number) => {
if (pagesLength % 4 !== 0) {
throw new Error('pages needs multiples of 4.')
}
const pages = []
let min = 1
let max = pagesLength
let isFront = true
while (min < max) {
if (isFront) {
pages.push(max, min)
} else {
pages.push(min, max)
}
isFront = !isFront
min++
max--
}
return pages
}
const readPagesLength = (filename: string) => {
const buf = childProcess.execSync(`pdfinfo ${filename}`)
// FIXME: need shell escape
for (const line of buf.toString().split('\n')) {
const arr = line.split(':')
if (arr[0] === 'Pages') {
return Number.parseInt(arr[1].trim())
}
}
console.log(buf.toString())
throw new Error('pages not found')
}
const run = (filename: string, outputFile:string) => {
const pagesLength = readPagesLength(filename)
console.log(`pdftk ${filename} cat ${calcImpotision(pagesLength).join(' ')} output ${outputFile}`)
}
run(process.argv[2], process.argv[3])