-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_pdf.js
71 lines (46 loc) · 1.88 KB
/
split_pdf.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
70
var extract = require('pdf-text-extract');
var hummus = require('hummus');
var path = require('path');
var fs = require('fs');
//parser
var scan_cf = require('./scan_cf.js');
module.exports = function(sourcePDF, outputFolder, callback){
console.log('starttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt');
var cf_list = "";
//extract pages in array of strings, one page per item
extract(sourcePDF, (err, pages) => {
//check extracting
if (err) console.log(err);
var scanned = "";
//use a for-loop to make it easier to break out after a match is found, a match is a string in the cf form.
for (let i = 0; i < pages.length; i++) {
scanned = scan_cf(pages[i]);
//use the hummus package to pull that page out into it's own document and use the cf code as the document name.
//If already exist, appending.
var exists = fs.existsSync(path.join(outputFolder, `${scanned}.pdf`));
if (exists){
var modify_writer = hummus.createWriterToModify(
path.join(outputFolder, `${scanned}.pdf`),
{modifiedFilePath: path.join(outputFolder, `${scanned}.pdf`)
});
modify_writer.appendPDFPagesFromPDF(
sourcePDF,
{type:hummus.eRangeTypeSpecific,specificRanges: [ [ i,i ] ]}
);
modify_writer.end();
}
else{
cf_list += scanned + ',';
var pdfWriter = hummus.createWriter(
path.join(outputFolder, `${scanned}.pdf`)
);
pdfWriter.appendPDFPagesFromPDF(
sourcePDF,
{type:hummus.eRangeTypeSpecific,specificRanges: [ [ i,i ] ]}
);
pdfWriter.end();
}
}
callback(cf_list);
});
}