-
Notifications
You must be signed in to change notification settings - Fork 8
/
SplitCombine.js
69 lines (64 loc) · 2.58 KB
/
SplitCombine.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const fs = require('mz/fs');
const withTmpDir = require('with-tmp-dir-promise').WithTempDir;
const exec = require('mz/child_process').exec;
const path = require('path');
const _ = require('lodash');
const {requireNativeExecutableSync} = require('require-native-executable');
requireNativeExecutableSync('gs');
/**
* Split PDF into individual pges
* @param {Buffer} The PDF to split (content, not filename)
* @return Promise of array of individual page PDFs
*/
async function splitPDF (pdfBuffer) {
return withTmpDir(async (tmpdir) => {
const inpath = path.join(tmpdir, 'in.pdf');
// Write input files
await fs.writeFile(inpath, pdfBuffer);
// Split. Burst into individual file
const cmd = `gs -sDEVICE=pdfwrite -dSAFER -dPDFSETTINGS=/prepress -o page.%08d.pdf in.pdf`;
await exec(cmd, {cwd: tmpdir});
// Read all the files, pg_0001.pdf, pg_0002.pdf
const files = await fs.readdir(tmpdir);
const pages = files.filter(file => _.startsWith(file, 'page.')).sort();
const pagePaths = pages.map(page => path.join(tmpdir, page));
const pagePromises = pagePaths.map(jpg => fs.readFile(jpg));
return Promise.all(pagePromises);
}, { unsafeCleanup: true, prefix: 'pdftoolz-split-' });
}
class PDFCombineError extends Error {}
/**
* Combine multiple documents into one PDF file.
* Usually this is used to combine multiple PDF files into one.
* @param {Buffer[]} The PDFs to combine.
* @return Promise of combined PDF
*/
async function combinePDF (pdfBuffers) {
if (pdfBuffers.length === 0) {
throw new PDFCombineError('Trying to combine a list of zero PDF buffers. Please add at least one PDF buffer!');
} else if (pdfBuffers.length === 1) {
// No need to combine
return pdfBuffers[0];
}
// Combine
return withTmpDir(async (tmpdir) => {
const outpath = path.join(tmpdir, `out.pdf`);
// Write input files & assemble command.
let cmd = `gs -sDEVICE=pdfwrite -dSAFER -dPDFSETTINGS=/prepress -o ${outpath}`;
const promises = [];
for (let i = 0; i < pdfBuffers.length; i++) {
const pdfPath = path.join(tmpdir, `in-${i}.pdf`);
promises.push(fs.writeFile(pdfPath, pdfBuffers[i]));
cmd += ` ${pdfPath}`;
}
await Promise.all(promises);
await exec(cmd);
// Read the result file
return fs.readFile(outpath);
}, { unsafeCleanup: true, prefix: 'pdftoolz-combine-' });
}
module.exports = {
splitPDF: splitPDF,
combinePDF: combinePDF,
PDFCombineError: PDFCombineError
};