-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsHTML2PDF.JQuery.js
240 lines (202 loc) · 11.4 KB
/
jsHTML2PDF.JQuery.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
jsHtmlToPDF.JQuery.js
Version 1.1.1 (9/8/2016)
Created by Luis Valle
JQuery plugin/wrapper to communicate with my COORS webService
and generate HTML to PDF. My webService uses the excellent wkhtmltopdf
tool to convert the HTML to PDF so all credit goes to those guys...
Visit www.evicore.net/jsHtmlToPDF.html for demos and setup details
*/
(function() {
this.jsHtmlToPDF = function(options) {
this.settings = null;
this.dEvicore = null;
this.dEvicore = $.Deferred();
settingsX = $.extend({
pageStyle: '',
myURLl: 'https://www.evicore.net/webservices/evicoreservice.asmx/postHTMLtoPDFJS',
myCustomParams: '',
fileName: '',
thisSessionID: ''
}, options );
this.settings = settingsX;
}
jsHtmlToPDF.prototype.convert = function() {
var _settings = this.settings;
var _dEvicore = this.dEvicore;
this.settings = null;
this.dEvicore = null;
_settings.waitDivElmnt = $("<div id=\"dvPDFTelWait\" style=\"position: fixed; width: 100%; height: 100%; top: 0px; left: 0px; background-color: black;z-index:999999; " +
"display: none;\"><form id=\"dvEvCrClone\"></form><\/div>");
var tmpSettngsss = $.parseJSON(_settings.myCustomParams);
if (tmpSettngsss.hasOwnProperty('fileformat')) {
if (tmpSettngsss.fileformat == 'pdf' || tmpSettngsss.fileformat == 'doc' || tmpSettngsss.fileformat == 'xls') {
} else {
tmpSettngsss.fileformat = 'pdf';
}
} else {
tmpSettngsss.fileformat = 'pdf';
}
var someRandomNum = ((Math.random() * 1e6) | 0);
_settings.thisSessionID = 'WebSession_' + someRandomNum;
_settings.fileName = ($.trim(_settings.fileName) != '' ? _settings.fileName : 'file_' + someRandomNum).replace(/\|uniqueNumber\|/g, someRandomNum);
$(_settings.waitDivElmnt).insertAfter($(document.body).children().last());
if ($.type(_settings.html) == 'string') {
$('#dvEvCrClone').append('<div>' + _settings.html + '</div>');
} else {
var _xConvertToStringFields = false;
if (tmpSettngsss.fileformat == 'doc' || tmpSettngsss.fileformat == 'xls') {
_xConvertToStringFields = true;
}
if (_xConvertToStringFields == false) {
$(_settings.html).clone().appendTo($('#dvEvCrClone'));
var $tElm = $(_settings.html);
var origTextAreas = $($tElm).find('textarea');
var newTextAreas = $('#dvEvCrClone').children(":first").find('textarea');
for (var i=0; i < newTextAreas.length; i++) {
var orgVallll = $(origTextAreas[i]).val();
$(newTextAreas[i]).val('');
$(newTextAreas[i]).append(orgVallll);
//$(newTextAreas[i]).append($(origTextAreas[i]).val());
}
var origSelects = $($tElm).find('select');
var newSelects = $('#dvEvCrClone').children(":first").find('select');
for (var i=0; i < newSelects.length; i++) {
var selVal = $('option:selected', origSelects[i]).val();
$(newSelects[i]).find('option').each(function () {
var xSelVal = $(this).val();
if (selVal == xSelVal) {
$(this).attr('selected', 'selected');
}
});
}
$('#dvEvCrClone input:text').each(function () {
$(this).attr('value', $(this).val());
});
$("#dvEvCrClone :checked").each(function () {
$(this).attr('checked', 'checked');
});
/* Try to set img src's to absolute paths so wkhtmltopdf can render them correctly */
if ($.trim(window.window.location.hostname) != '') {
var tmpThisProtocol = $(location).attr('protocol');
var tmpThisHostName = $(location).attr('hostname');
$('#dvEvCrClone img').each(function () {
if (!$(this).attr('src').match(';base64,')) {
if (!$(this).attr('src').match('http') && !$(this).attr('src').match('://')) {
$(this).attr('src', tmpThisProtocol + '//' + tmpThisHostName + '/' + $(this).attr('src'));
}
}
});
}
$tElm = null;
} else {
$(_settings.html).clone().appendTo($('#dvEvCrClone'));
var $tElm = $(_settings.html);
var origTextAreas = $($tElm).find('textarea');
var newTextAreas = $('#dvEvCrClone').children(":first").find('textarea');
for (var i=0; i < newTextAreas.length; i++) {
var orgVallll = $(origTextAreas[i]).val();
orgVallll = orgVallll.replace(/\n/g, '<br />');
orgVallll = ($.trim(orgVallll) == '' ? ' ' : orgVallll);
//$(newTextAreas[i]).val('');
//$(newTextAreas[i]).append(orgVallll);
$(newTextAreas[i]).replaceWith('<div style="text-align:left;padding:2px;background-color:white;border:1px solid #919292;">' + orgVallll + '</div>');
//$(newTextAreas[i]).append($(origTextAreas[i]).val());
}
var origSelects = $($tElm).find('select');
var newSelects = $('#dvEvCrClone').children(":first").find('select');
for (var i=0; i < newSelects.length; i++) {
var selVal = $('option:selected', origSelects[i]).val();
var selTxt = $('option:selected', origSelects[i]).html();
selTxt = ($.trim(selTxt) == '' ? ' ' : selTxt);
$(newSelects[i]).replaceWith('<span style="padding:2px;background-color:white;border:1px solid #919292;">' + selTxt + '</span>');
//$(newSelects[i]).find('option').each(function () {
// var xSelVal = $(this).val();
// if (selVal == xSelVal) {
// $(this).attr('selected', 'selected');
// }
//});
}
$('#dvEvCrClone input:text').each(function () {
var xxVal = $(this).val();
xxVal = ($.trim(xxVal) == '' ? ' ' : xxVal);
//$(this).attr('value', $(this).val());
$(this).replaceWith('<span style="padding:2px;background-color:white;border:1px solid #919292;">' + xxVal + '</span>');
});
$("#dvEvCrClone :checkbox").each(function () {
var chkVall = ($(this).is(':checked') ? '✓' : ' ');
//$(this).attr('checked', 'checked');
$(this).replaceWith('<span style="background-color:white;border:1px solid #919292;">' + chkVall + '</span>');
});
$("#dvEvCrClone :radio").each(function () {
var chkVall = ($(this).is(':checked') ? '✓' : ' ');
//$(this).attr('checked', 'checked');
$(this).replaceWith('<span style="background-color:white;border:1px solid #919292;">' + chkVall + '</span>');
});
/* Try to set img src's to absolute paths so wkhtmltopdf can render them correctly */
if ($.trim(window.window.location.hostname) != '') {
var tmpThisProtocol = $(location).attr('protocol');
var tmpThisHostName = $(location).attr('hostname');
$('#dvEvCrClone img').each(function () {
if (!$(this).attr('src').match(';base64,')) {
if (!$(this).attr('src').match('http') && !$(this).attr('src').match('://')) {
$(this).attr('src', tmpThisProtocol + '//' + tmpThisHostName + '/' + $(this).attr('src'));
}
}
});
}
$tElm = null;
}
}
var $tHml = (_settings.pageStyle + $('#dvEvCrClone').children().first().html()).replace(/\</g, '⌐').replace(/\>/g, '¬');
$('#dvEvCrClone').remove();
_settings.html = null;
origTextAreas = null;
newTextAreas = null;
origSelects = null;
newSelects = null;
$('body').append("<form method='POST' action='" + _settings.myURLl + "' style='position:fixed;top:-3333333333px;'" +
" id='tempForm'><input" +
" type='hidden' id='inHTML' name='inHTML' value='' /><input type='hidden'" +
" id='properties' name='properties' value='' /><input" +
" type='hidden' id='FileNamex' name='FileNamex' value='' /><input" +
" type='submit' value='Submit' style='opacity:0;' class='HiddenBtn' /></form>");
$('#inHTML').val($tHml);
$('#properties').val(_settings.myCustomParams);
$('#FileNamex').val(_settings.fileName);
$('#tempForm').submit();
setTimeout(function () {
$('#tempForm').remove();
$('#dvPDFTelWait').remove();
$(_settings.waitDivElmnt).remove();
_dEvicore.resolve(true);
}, 2000);
$tHml = '';
return _dEvicore.promise();
}
}());
/* Support for Stringifying JSON objects for older versions of IE. Credit goes to http://blogs.sitepointstatic.com/examples/tech/json-serialization/json-serialization.js */
(function() {
jQuery.extend({
stringify : function stringify(obj) {
var t = typeof (obj);
if (t != "object" || obj === null) {
// simple data type
if (t == "string") obj = '"' + obj + '"';
return String(obj);
} else {
// recurse array or object
var n, v, json = [], arr = (obj && obj.constructor == Array);
for (n in obj) {
v = obj[n];
t = typeof(v);
if (obj.hasOwnProperty(n)) {
if (t == "string") v = '"' + v + '"'; else if (t == "object" && v !== null) v = jQuery.stringify(v);
json.push((arr ? "" : '"' + n + '":') + String(v));
}
}
return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
}
}
});
}());