Skip to content

Commit b29b81a

Browse files
committed
Add base64decode functions from phpjs
1 parent 96a88ee commit b29b81a

1 file changed

Lines changed: 60 additions & 2 deletions

File tree

jspdf.js

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var jsPDF = (function() {
4242
// this will run on <=IE9, possibly some niche browsers
4343
// new webkit-based, FireFox, IE10 already have native version of this.
4444
if (typeof btoa === 'undefined') {
45-
var btoa = function(data) {
45+
window.btoa = function(data) {
4646
// DO NOT ADD UTF8 ENCODING CODE HERE!!!!
4747

4848
// UTF8 encoding encodes bytes over char code 128
@@ -124,7 +124,65 @@ if (typeof btoa === 'undefined') {
124124
return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);
125125

126126
// end of base64 encoder MIT, GPL
127-
}
127+
};
128+
}
129+
130+
if (typeof atob === 'undefined') {
131+
window.atob = function (data) {
132+
// http://kevin.vanzonneveld.net
133+
// + original by: Tyler Akins (http://rumkin.com)
134+
// + improved by: Thunder.m
135+
// + input by: Aman Gupta
136+
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
137+
// + bugfixed by: Onno Marsman
138+
// + bugfixed by: Pellentesque Malesuada
139+
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
140+
// + input by: Brett Zamir (http://brett-zamir.me)
141+
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
142+
// * example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
143+
// * returns 1: 'Kevin van Zonneveld'
144+
// mozilla has this native
145+
// - but breaks in 2.0.0.12!
146+
//if (typeof this.window['atob'] == 'function') {
147+
// return atob(data);
148+
//}
149+
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
150+
var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
151+
ac = 0,
152+
dec = "",
153+
tmp_arr = [];
154+
155+
if (!data) {
156+
return data;
157+
}
158+
159+
data += '';
160+
161+
do { // unpack four hexets into three octets using index points in b64
162+
h1 = b64.indexOf(data.charAt(i++));
163+
h2 = b64.indexOf(data.charAt(i++));
164+
h3 = b64.indexOf(data.charAt(i++));
165+
h4 = b64.indexOf(data.charAt(i++));
166+
167+
bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
168+
169+
o1 = bits >> 16 & 0xff;
170+
o2 = bits >> 8 & 0xff;
171+
o3 = bits & 0xff;
172+
173+
if (h3 == 64) {
174+
tmp_arr[ac++] = String.fromCharCode(o1);
175+
} else if (h4 == 64) {
176+
tmp_arr[ac++] = String.fromCharCode(o1, o2);
177+
} else {
178+
tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
179+
}
180+
} while (i < data.length);
181+
182+
dec = tmp_arr.join('');
183+
184+
return dec;
185+
};
128186
}
129187

130188
var getObjectLength = typeof Object.keys === 'function' ?

0 commit comments

Comments
 (0)