-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.js
46 lines (38 loc) · 1.35 KB
/
build.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
var fs = require('fs');
var path = require('path');
var sizeOf = require('image-size');
var PDFDocument = require('pdfkit');
var dpi = 300;
module.exports = function (targetDir, filename, format, callback) {
console.log("Rendering PDF");
fs.readdir(targetDir, function (err, files) {
if (err) {
return callback(err);
}
var doc, stream, width, height;
files.forEach(function(file){
if (path.extname(file) === '.' + format) {
var filepath = targetDir + '/' + file;
if (!doc) {
var res = sizeOf(filepath);
width = calcPdfPoints(res.width || 960, dpi);
height = calcPdfPoints(res.height || 700, dpi);
doc = new PDFDocument({layout: 'landscape', margin: 0, size: [height, width]});
stream = fs.createWriteStream(targetDir + '/' + filename + '.pdf');
doc.pipe(stream);
doc.on('error', function(err){
throw err;
})
} else {
doc.addPage();
}
doc.image(filepath, 0, 0, {fit: [width, height]});
}
});
doc.end();
callback();
});
};
var calcPdfPoints = function(res, dpi) {
return res / dpi * 72;
};