-
Notifications
You must be signed in to change notification settings - Fork 0
/
rendersvgs.js
72 lines (59 loc) · 2.58 KB
/
rendersvgs.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
/*
* Demo that renders a transcript using phantomjs. Needs to be run with
* phantomws, obviously.
*/
window.setTimeout(function() {
console.log("ERROR: Timeout during conversion for " + input);
phantom.exit(2);
}, 300000);
var page = require('webpage').create(),
fs = require('fs'),
system = require('system');
var backend = system.args[1],
input = system.args[2],
output = system.args[3],
links = system.args[4],
linkout = system.args[5];
if (system.args.length !== 4 && system.args.length !== 6) {
console.log('Usage: rendersvgs.js backend input output [links linkoutput]');
console.log(' backend: http URL to the transcript-generation.html');
console.log(' input: JSON file of the page transcript');
console.log(' output: output svg file, will be written');
console.log(' links: text image link svg, if given, will write overlay svg');
phantom.exit(1);
}
page.onError = function (msg, trace) { console.log("ENGINE ERROR:", msg, typeof(msg), arguments); trace.forEach(function(item) { console.log(' at ', item.file, ':', item.line); }); };
page.onConsoleMessage = function(msg, lineNum, sourceId, level, fun) { console.log('CONSOLE: ' + msg + ' (from '+fun+', line #' + lineNum + ' in "' + sourceId + '")'); };
page.onCallback = function(result) {
var out;
out = fs.open(output, { mode: "w", charset: "UTF-8" });
out.write(result.svg);
out.close();
if (linkout) {
out = fs.open(linkout, { mode: "w", charset: "UTF-8" });
out.write(result.overlay);
out.close();
}
/* out = fs.open(output + ".html", { mode: "w", charset: "UTF-8" });
out.write(page.content);
out.close();
page.evaluate(function() {document.getElementsByTagName('body')[0].style.zoom = 0.62;});
*/
page.paperSize = { format: "A4", orientation: "portrait", margin: "6pt" };
page.render(output.replace(/.svg$/, ".pdf"));
page.render(output.replace(/.svg$/, ".png"));
phantom.exit();
};
page.open(backend, function(status) {
var transcript = fs.read(input, { mode: "r", charset: "UTF-8"}),
imagelinks = links? fs.read(links, {mode: "r", charset: "UTF-8"}) : undefined;
function render(transcript, imagelinks) {
var preloadEl = document.getElementById('preload');
preloadEl.parentNode.removeChild(preloadEl);
transcriptGeneration.createToPhantom(transcript, imagelinks);
};
window.setTimeout(function() {
page.render("target/preload.png");
window.setTimeout(function() { page.evaluate(render, transcript, imagelinks); }, 1500);
}, 1000);
});