-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (45 loc) · 1.49 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
const CPUs = require('os').cpus().length;
const { fork } = require('child_process');
// eslint-disable-next-line
const pdfjsLib = require('pdfjs-dist/legacy/build/pdf.js');
const { mkdir, readFile } = require('fs/promises');
const { checkAccessibility, convertToArray, splitPages } = require('./utilities');
const density = 300;
const format = 'png';
const size = '1240x1754'; // A4 / 2, full A4 is 2480x3508
async function launch() {
const [path = ''] = process.argv.slice(2);
if (!path) {
throw new Error('Missing path to the file!');
}
await checkAccessibility(path);
const [saveDirName] = path.split('/').slice(-1)[0].split('.');
const partials = path.split('/');
const savePath = `${partials.slice(0, partials.length - 1).join('/')}/${saveDirName}`;
const file = await readFile(path);
const { numPages: totalPages = null } = await pdfjsLib.getDocument(file).promise;
if (!totalPages) {
throw new Error('Cannot process provided file!');
}
await mkdir(savePath);
const [width, height] = size.split('x');
const pages = convertToArray(totalPages);
const threads = convertToArray(CPUs);
const workload = splitPages(pages, threads);
for (let i = 0; i < workload.length; i += 1) {
const converter = fork('converter.js');
converter.send({
options: {
density,
format,
height: Number(height),
savePath,
width: Number(width),
},
pages: [...workload[i]],
source: path,
worker: i,
});
}
}
launch();