-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (97 loc) · 2.71 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
var fs = require('fs');
var generator = require('./src/html-generate');
var importer = require('./src/workspace-import');
var path = require('path');
function importAndGenerate(options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
importer.import(function(cueLists) {
var html = generator.generate(cueLists, options);
cb(html);
});
}
module.exports.generateHTML = importAndGenerate;
module.exports.exportHTML = function(htmlPath, options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
importer.import(function(cueLists) {
var html = generator.generate(cueLists, options);
if (!htmlPath) {
htmlPath = `${cueLists.workspaceName}.html`;
} else {
var pathObj = path.parse(htmlPath);
pathObj.ext = '.html';
pathObj.base = undefined;
htmlPath = path.format(pathObj);
console.log(htmlPath)
}
fs.writeFile(htmlPath, html, function(err) {
if (cb) {
cb(err, htmlPath, html);
}
});
});
}
module.exports.convertHTMLtoPDF = function(html, landscape, cb) {
try {
var CDP = require('chrome-remote-interface');
} catch (err) {
CDP = null;
}
try {
var chromeRunner = require('chrome-runner');
} catch (err) {
chromeRunner = null;
}
if (!CDP) {
console.error('chrome-remote-interface not installed, please run `npm install chrome-remote-interface`');
} else if (!chromeRunner) {
console.error('chrome-runner not installed, please run `npm install chrome-runner`');
} else {
chromeRunner.launchWithHeadless().then(function(runner) {
CDP({
port: runner.port
}, function(client) {
var Network = client.Network;
var Page = client.Page;
// enable events then start!
Promise.all([
Page.enable()
]).then(function() {
return Page.navigate({
url: 'about:blank'
})
}).then(function(results) {
var frameId = results.frameId;
return Page.setDocumentContent({
frameId: frameId,
html: html
})
}).then(function() {
return Page.printToPDF({
landscape: landscape || false,
displayHeaderFooter: false,
printBackground: true/*,
marginTop: 0,
marginBottom: 0,
marginLeft: 0,
marginRight: 0*/
})
}).then(function(pdf) {
cb(Buffer.from(pdf.data, 'base64'));
client.close();
runner.kill();
}).catch(function(err) {
console.error(err);
client.close();
});
}).on('error', function(err) {
console.error(err);
});
});
}
}