-
Notifications
You must be signed in to change notification settings - Fork 5
/
imgtopdf.js
58 lines (49 loc) · 1.42 KB
/
imgtopdf.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
PDFDocument = require('pdfkit');
const argv = require('minimist')(process.argv.slice(2));
var input="./img";
var output="./prezi.pdf";
if("im" in argv) input = argv.im;
if("out" in argv) output = argv.out;
fs = require('fs');
doc = new PDFDocument({
autoFirstPage: false
});
imgs = [];
doc.pipe(fs.createWriteStream(output));
console.log("Creating pdf",output);
fs.readdir(input, function (err, files) {
//handling error
if (err) {
return console.log('Unable to scan directory: ' + err);
}
//listing all files using forEach
files.forEach(function (file) {
// Do whatever you want to do with the file
imgs.push(file.substring(6).replace(".png",""));
});
x = imgs.sort(function(a, b) {
return a - b;
});
for(var i in x){
console.log(`writing Image #${i} to pdf`);
var img = doc.openImage(`${input}/prezi-${i}.png`);
doc.addPage()
.image(`${input}/prezi-${i}.png`, {
fit: [img.width, img.height]
});
doc.image(img, 0, 0);
}
console.log("saving to",output);
doc.end();
// delete files if wished
if("del" in argv){
fs.readdir(input, (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(path.join(input, file), err => {
if (err) throw err;
});
}
});
}
});