-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
65 lines (58 loc) · 2.5 KB
/
index.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
#!/usr/bin/env node
const pkg = require('./package.json');
const updateNotifier = require('update-notifier')({pkg});
if(updateNotifier.update){
updateNotifier.notify({defer:false}); // display plz-update notification message
setTimeout(main, 5000); // start after 5 seconds
}else{
main(); // up to date - start right away
}
function main(){
console.log("v" + pkg.version + " - coded Nov17 by Lars Froelich\n");
console.log("*** Merge-PDFs ***");
const cwd = process.cwd();
// configure program-parameters
var program = require('commander')
.option('-e, --even', 'Add empty pages to PDFs that end on odd pages (useful for manual duplex printing)')
.parse(process.argv);
console.log(" - loading dependencies");
const pdfjs = require('pdfjs');
const fs = require("fs");
const readdir = require("recursive-readdir");
console.log(" - loading font");
var helvetica = fs.readFileSync(__dirname + '/Helvetica.json');
helvetica = JSON.parse(helvetica);
var helveticaf = new pdfjs.Font(helvetica);
console.log(" - creating output pdf");
var doc = new pdfjs.Document({font: helveticaf});
var totalCount = 0;
var evenCount = 0;
console.log(" - adding PDFs");
readdir(cwd, ["!*.pdf", "merged.pdf"]).then( // get all pdf-files except previous merges
function(pdfs) {
pdfs.forEach(function(pdf){
console.log(" + adding \"" + pdf.replace(/^.*[\\\/]/, '') + "\" (" + pdf + ")");
var file = fs.readFileSync(pdf);
var ext = new pdfjs.ExternalDocument(file);
doc.addPagesOf(ext);
totalCount += ext.pageCount;
if(program.even && (ext.pageCount % 2 === 1)){
doc.text(' . '); // some printers don't "skip" this page if it's entirely empty.
totalCount ++;
evenCount ++;
}
});
console.log(" - writing output-file");
doc.pipe(fs.createWriteStream(cwd + '/merged.pdf'));
doc.end().then(function () {
console.log("\n Finished writing PDF (" + totalCount + " pages total).\n Merged output is 'merged.pdf'");
if(program.even){
console.log(" " + evenCount + " empty pages have been added to PDFs with odd numbers of pages.");
}
});
},
function(error) {
console.error("Unable to search for pdf-files ", error);
}
);
}