Created
December 5, 2017 16:48
-
-
Save d-asensio/eafa28b516379923012d865ba13926ab to your computer and use it in GitHub Desktop.
This file contains 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
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