forked from arve0/codeclub_lesson_builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf.js
47 lines (44 loc) · 1.47 KB
/
pdf.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
var glob = require('glob');
var path = require('path');
var each = require('async-each');
var _ = require('lodash');
var PDF = require('nodepdf-series');
var config = require('./config.js');
var concurrent = 4; // how many processes to spawn
var pdfOptions = { 'viewportSize': { 'width': 1024 } };
var generatePdf = function(done){
glob(config.buildRoot + '/**/*.html', function(e, files){
if (e) {
done(e);
return;
}
files = files.map(function(file){
return 'file://' + path.resolve(file);
});
var chunkSize = Math.round(files.length/concurrent);
var chunks = _.chunk(files, chunkSize);
each(chunks, function(chunk, cb){
PDF.render(chunk, pdfOptions, function(err){
cb(err);
});
}, function(err){
// check if all htmls are converted
glob(config.buildRoot + '/**/*.pdf', function(e, pdfs){
pdfs = pdfs.map(function(pdf){
return path.resolve(pdf);
});
var diff = files.map(function(file){
file = file.replace('file://', '').replace('.html', '.pdf');
if (pdfs.indexOf(file) == -1) return file;
});
diff = _.compact(diff); // remove falsey values
if (diff.length !== 0) {
var lengthErr = new Error('Not all htmls was converted to pdf. ' +
'Missing pdfs:\n' + JSON.stringify(diff, null, ' '));
}
done(err || e || lengthErr);
});
});
});
};
module.exports = generatePdf;