-
Notifications
You must be signed in to change notification settings - Fork 5
/
html2pdf.js
110 lines (97 loc) · 2.41 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* html2pdf.js
* Copyright (c) 2014 Steven Spungin (TwelveTone LLC) [email protected]
*
* Licensed under the MIT License.
* http://opensource.org/licenses/mit-license
*/
function html2pdf (html,pdf,callback) {
var canvas = pdf.canvas;
if (!canvas) {
alert('jsPDF canvas plugin not installed');
return;
}
canvas.pdf = pdf;
pdf.annotations = {
_nameMap : [],
createAnnotation : function(href,bounds) {
var x = pdf.context2d._wrapX(bounds.left);
var y = pdf.context2d._wrapY(bounds.top);
var page = pdf.context2d._page(bounds.top);
var options;
var index = href.indexOf('#');
if (index >= 0) {
options = {
name : href.substring(index + 1)
};
} else {
options = {
url : href
};
}
pdf.link(x, y, bounds.right - bounds.left, bounds.bottom - bounds.top, options);
},
setName : function(name,bounds) {
var x = pdf.context2d._wrapX(bounds.left);
var y = pdf.context2d._wrapY(bounds.top);
var page = pdf.context2d._page(bounds.top);
this._nameMap[name] = {
page : page,
x : x,
y : y
};
}
};
canvas.annotations = pdf.annotations;
pdf.context2d._pageBreakAt = function(y) {
this.pageBreaks.push(y);
};
pdf.context2d._gotoPage = function(pageOneBased) {
while (pdf.internal.getNumberOfPages() < pageOneBased) {
pdf.addPage();
}
pdf.setPage(pageOneBased);
}
if (typeof html === 'string') {
// remove all scripts
html = html.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
var iframe = document.createElement('iframe');
//iframe.style.width = canvas.width;
//iframe.src = "";
//iframe.document.domain =
document.body.appendChild(iframe);
var doc;
doc = iframe.contentDocument;
if (doc == undefined || doc == null) {
doc = iframe.contentWindow.document;
}
//iframe.setAttribute('style', 'position:absolute;right:0; top:0; bottom:0; height:100%; width:500px');
doc.open();
doc.write(html);
doc.close();
var promise = html2canvas(doc.body, {
canvas : canvas,
onrendered : function(canvas) {
if (callback) {
if (iframe) {
iframe.parentElement.removeChild(iframe);
}
callback(pdf);
}
}
});
} else {
var body = html;
var promise = html2canvas(body, {
canvas : canvas,
onrendered : function(canvas) {
if (callback) {
if (iframe) {
iframe.parentElement.removeChild(iframe);
}
callback(pdf);
}
}
});
}
}