-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveToPdf.js
89 lines (79 loc) · 2.17 KB
/
saveToPdf.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
var page = require('webpage').create();
var system = require('system');
if (system.args.length < 10 || system.args.length > 10) {
console.log('Usage: saveToPdf.js URL API locale username password userKey from to output');
phantom.exit();
}
else {
const address = system.args[1];
const api= system.args[2];
const locale = system.args[3];
const username = system.args[4];
const password = system.args[5];
const userKey = system.args[6];
const from = system.args[7];
const to = system.args[8];
const output = system.args[9];
const dpi = 96;
const data = {
username:username,
password:password,
userKey: userKey,
from:from,
to:to,
locale:locale,
api:api
};
const pageWidth = 8.3*dpi;
const pageHeight = 11.7*dpi;
page.viewportSize = { width: pageWidth, height: pageHeight };
page.clipRect = { top: 0, left: 0, width: pageWidth, height: pageHeight };
page.paperSize = {
format: 'A4',
orientation: 'portrait',
margin: '2.5cm',
footer: {
height: '1.25cm',
contents: phantom.callback(function(pageNum, numPages) {
if (numPages > 1) {
return "<div style='font-size:0.8em'><span style='color:#666; float:right'>" + pageNum + "/" + numPages + "</span></div>";
}
else {
return "";
}
})
}
};
page.onConsoleMessage = function(msg) {
console.log(msg);
}
var errors = [];
page.onError = function(msg, trace) {
errors.push(msg);
console.log('\n' + msg);
}
const settings = {
operation: "POST",
encoding: "utf8",
headers: {
"Content-Type": "application/json"
},
data: JSON.stringify(data)
};
page.open(address, settings, function(status) {
console.log('\nOpened page with status:', status);
if (status !== 'success') {
console.error('Unable to execute POST request');
phantom.exit();
}
window.setTimeout(function() {
page.render(output);
if (errors.length === 0) {
console.log('\nRendered PDF successfully!');
} else {
console.error('\nRender Failure. Found ' + errors.length + ' error(s)');
}
phantom.exit();
}, 300);
});
}