forked from parallax/jsPDF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_harness.js
More file actions
161 lines (137 loc) · 4.51 KB
/
Copy pathtest_harness.js
File metadata and controls
161 lines (137 loc) · 4.51 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* jsPDF PDF Test Harness
* Copyright (c) 2014 Steven Spungin (TwelveTone LLC) steven@twelvetone.tv
*
* Licensed under the MIT License.
* http://opensource.org/licenses/mit-license
*/
/**
* An easy way to view PDF and PDF source code side by side.
*/
pdf_test_harness_init = function(pdf, message) {
var harness = new pdf_test_harness();
var body = document.getElementsByTagName('body')[0];
body.style.display = 'flex';
var div = document.createElement('div');
div.setAttribute('style', 'position:fixed;height:20px;left:0;right:0;background:lightblue');
body.appendChild(div);
harness.header = div;
var div2 = document.createElement('div');
div2.setAttribute('style', 'position:fixed;display:flex;top:20px; bottom:0;left:0;right:0');
body.appendChild(div2);
harness.body = div2;
var btn1 = document.createElement('input');
btn1.setAttribute('type', 'radio');
btn1.setAttribute('name', 'view');
div.appendChild(btn1);
btn1.checked = true;
var lbl1 = document.createElement('label');
lbl1.setAttribute('for', 'btn1');
lbl1.innerHTML = 'PDF'
div.appendChild(lbl1);
var btn2 = document.createElement('input');
btn2.setAttribute('type', 'radio');
btn2.setAttribute('name', 'view');
div.appendChild(btn2);
var lbl2 = document.createElement('label');
lbl2.setAttribute('for', 'btn2');
lbl2.innerHTML = 'Source'
div.appendChild(lbl2);
var btn3 = document.createElement('input');
btn3.setAttribute('type', 'radio');
btn3.setAttribute('name', 'view');
div.appendChild(btn3);
var lbl3 = document.createElement('label');
lbl3.setAttribute('for', 'btn3');
lbl3.innerHTML = 'Both'
div.appendChild(lbl3);
harness.source = document.createElement('pre');
harness.source.setAttribute('style', 'margin-top:0;width:100%;height:100%;position:absolute;top:0px;bottom:0px;overflow:auto');
div2.appendChild(harness.source);
harness.iframe = document.createElement('iframe');
harness.iframe.setAttribute('style', 'width:100%;height:100%;position:absolute;overflow:auto;top:0px;bottom:0px');
div2.appendChild(harness.iframe);
//if (pdf_test_harness.onload) {
//harness.pdf = pdf_test_harness.onload(harness);
if (message) {
message += "<p style='text-align:center;font-style:italic;font-size:.8em'>click to close</p>";
var popup = document.createElement('div');
popup.setAttribute('style', 'z-index:100;margin:100px auto;cursor:pointer;font-size:1.3em;top:50px;background-color:rgb(243, 224, 141);padding:1em;border:1px solid black');
popup.innerHTML = message;
body.appendChild(popup);
popup.onclick = function() {
popup.parentNode.removeChild(popup);
}
}
//}
harness.pdf = pdf;
harness.render('pdf');
btn1.onclick = function() {
harness.render('pdf');
}
btn2.onclick = function() {
harness.render('source');
}
btn3.onclick = function() {
harness.render('both');
}
return harness;
}
pdf_test_harness = function(pdf) {
this.pdf = pdf;
this.onload = undefined;
this.iframe = undefined;
this.entityMap = {
"&" : "&",
"<" : "<",
">" : ">",
'"' : '"',
"'" : ''',
"/" : '/'
};
this.escapeHtml = function(string) {
return String(string).replace(/[&<>"'\/]/g, function(s) {
return this.entityMap[s];
}.bind(this));
};
this.getParameterByName = function(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
};
this.setPdf = function(pdf) {
this.pdf = pdf;
this.rendered = undefined;
this.render(this.view);
};
// generate the pdf, the source code, or both
this.render = function(view) {
this.view = view;
//Current code only lets us render one time.
if (!this.rendered) {
this.rendered = this.pdf.output('datauristring');
this.iframe.src = this.rendered;
var raw = this.pdf.output();
raw = this.escapeHtml(raw);
this.source.innerHTML = raw;
}
if ('pdf' === view) {
this.source.style.display = 'none';
this.iframe.style.display = 'block';
this.iframe.style.width = '100%';
} else if ('source' === view) {
this.iframe.style.display = 'none';
this.source.style.display = 'block';
this.source.style.width = '100%';
}
if ('both' === view) {
raw = this.escapeHtml(raw);
this.iframe.style.width = '50%';
this.iframe.style.position = 'relative';
this.iframe.style.display = 'inline-block';
this.source.style.width = '50%';
this.source.style.position = 'relative';
this.source.style.display = 'inline-block';
}
}
}