-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·88 lines (70 loc) · 2.24 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env node
import { program } from "commander";
import path from "path";
import gs from "ghostscript-node";
import fs from "fs-extra";
program
.command("compress")
.alias("cp")
.argument("[file]", "PDF file for compress")
.description("Compress pdf file")
.action(async function (file) {
try {
console.log("Processing...");
const pdf = fs.readFileSync(file);
const output = addStringToFilename(file, "compressed");
const compressed = await gs.compressPDF(pdf);
fs.appendFileSync(output, Buffer.from(compressed));
console.log("Complete!");
} catch (error) {
console.error(error.message);
}
});
program
.command("split")
.alias("s")
.argument("[file]", "PDF file for split")
.argument("[start]", "Start page", 0)
.argument("[last]", "Last page")
.description("Split pdf file")
.action(async function (file, start, last) {
try {
console.log("Processing...");
const pdf = fs.readFileSync(file);
const lastPage = last == null ? await gs.countPDFPages(pdf) : last;
const output = addStringToFilename(file, start + "-" + lastPage);
const extract = await gs.extractPDFPages(pdf, start, lastPage);
fs.appendFileSync(output, Buffer.from(extract));
console.log("Complete!");
} catch (error) {
console.error(error.message);
}
});
program
.command("combine")
.alias("cb")
.argument("[file1]", "First PDF file for combine")
.argument("[file2]", "Second PDF file for combine")
.description("Combine pdf file")
.action(async function (file1, file2) {
console.log("Processing...");
try {
const pdf1 = fs.readFileSync(file1);
const pdf2 = fs.readFileSync(file2);
const output = addStringToFilename(file1, "combine");
const combine = await gs.combinePDFs([pdf1, pdf2]);
fs.appendFileSync(output, Buffer.from(combine));
console.log("Complete!");
} catch (error) {
console.error(error.message);
}
});
program.parse(process.argv);
if (program.args.length === 0) {
program.help();
}
function addStringToFilename(filename, addedString) {
const ext = path.extname(filename);
const basename = path.basename(filename, ext);
return `${basename}_${addedString}${ext}`;
}