Skip to content

Instantly share code, notes, and snippets.

@d-asensio
Created December 5, 2017 16:48
Show Gist options
  • Save d-asensio/eafa28b516379923012d865ba13926ab to your computer and use it in GitHub Desktop.
Save d-asensio/eafa28b516379923012d865ba13926ab to your computer and use it in GitHub Desktop.
const fs = require('fs-extra')
const path = require('path')
const chalk = require('chalk')
const scissors = require('scissors')
const INPUT_FOLDER = './input/'
const OUTPUT_FOLDER = './output/'
fs.readdirSync(INPUT_FOLDER).forEach(fileName => {
let pdf = scissors(path.join(INPUT_FOLDER, fileName))
pdf.getNumPages().then((numPages) => {
let outputSubdir = path.join(OUTPUT_FOLDER, path.basename(fileName, '.pdf'))
fs.ensureDir(outputSubdir, err => {
console.log('Created', outputSubdir)
})
for (let i = 1 ; i < numPages ; i +=2) {
let pdfread = scissors(path.join(INPUT_FOLDER, fileName))
let startPage = i
let endPage = startPage+1
let outputSlice = path.join(outputSubdir , `${startPage}-${endPage}.pdf`)
pdfread.pages(startPage, endPage)
.pdfStream()
.pipe(
fs.createWriteStream(outputSlice)
)
.on('finish', () => {
console.log(chalk.green('DONE'))
})
.on('error', () => {
console.log(chalk.red('FAIL'))
})
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment