-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtml2pdf.js
executable file
·95 lines (89 loc) · 2.71 KB
/
html2pdf.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
// https://github.com/ariya/phantomjs/wiki/API-Reference
// http://we-love-php.blogspot.fr/2012/12/create-pdf-invoices-with-html5-and-phantomjs.html
// http://dev.w3.org/csswg/css-page/#populating-margin-boxes
var page = new WebPage();
var system = require("system");
var fs = require('fs');
var id = system.args[1];
var host = system.args[2];
var size = 0;
var options;
var page_size = 'A4';
var orientation = 'portrait';
var margin_left = 25;
var margin_right = 25;
var margin_top = 10;
var margin_bottom = 10;
var header_align = 'left';
var header_text = '';
var header_pagination = 0;
var footer_align = 'left';
var footer_text = '';
var footer_pagination = 0;
var json_file = 'roadbook/' + id + '.json';
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
if (fs.exists(json_file) && fs.size(json_file) > 0) {
var plainOptions = fs.read(json_file);
options = JSON.parse(plainOptions);
if (options && typeof options == 'object') {
page_size = options.page_size;
orientation = options.orientation;
margin_left = parseInt(options.margin_left);
margin_right = parseInt(options.margin_right);
margin_top = parseInt(options.margin_top);
margin_bottom = parseInt(options.margin_bottom);
header_align = escapeHtml(options.header_align);
header_text = escapeHtml(options.header_text);
header_pagination = !! options.header_pagination;
footer_align = escapeHtml(options.footer_align);
footer_text = escapeHtml(options.footer_text);
footer_pagination = !! options.footer_pagination;
}
}
page.paperSize = {
format: page_size,
orientation: orientation,
margin: {
left: margin_left + 'mm',
right: margin_right + 'mm',
top: margin_top + 'mm',
bottom: margin_bottom + 'mm'
},
header: {
height: "0.9cm",
contents: phantom.callback(function(pageNum, numPages) {
var content = "<div style='font-size:10pt;text-align:" + header_align + ";'>";
if (header_pagination) {
content += pageNum + "/" + numPages;
} else {
content += header_text;
}
content += "</div>";
return content;
})
},
footer: {
height: "0.9cm",
contents: phantom.callback(function(pageNum, numPages) {
var content = "<div style='font-size:10pt;text-align:" + footer_align + ";'>";
if (footer_pagination) {
content += pageNum + "/" + numPages;
} else {
content += footer_text;
}
content += "</div>";
return content;
})
}
};
page.open('http://' + host + '/roadbook/' + id + '?raw', function(status) {
page.render('roadbook/pdf/' + id + '.pdf');
phantom.exit();
});