forked from parallax/jsPDF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_generate_tests.js
More file actions
executable file
·405 lines (323 loc) · 9.84 KB
/
Copy pathpdf_generate_tests.js
File metadata and controls
executable file
·405 lines (323 loc) · 9.84 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
$(document).ready(function(){
var dump = false // turn to true to dump contents of PDFs into textareas instead.
if (typeof atob === 'undefined') {
atob = function (data) {
// Decodes string using MIME base64 algorithm
//
// version: 1109.2015
// discuss at: http://phpjs.org/functions/base64_decode
// + original by: Tyler Akins (http://rumkin.com)
// + improved by: Thunder.m
// + input by: Aman Gupta
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Onno Marsman
// + bugfixed by: Pellentesque Malesuada
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// - depends on: utf8_decode
// * example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
// * returns 1: 'Kevin van Zonneveld'
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var o1, o2, o3, h1, h2, h3, h4, bits,
ac = 0,
i = 0,
l = data.length
tmp_arr = [];
do { // unpack four hexets into three octets using index points in b64
h1 = b64.indexOf(data.charAt(i++));
h2 = b64.indexOf(data.charAt(i++));
h3 = b64.indexOf(data.charAt(i++));
h4 = b64.indexOf(data.charAt(i++));
bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
o1 = bits >> 16 & 0xff;
o2 = bits >> 8 & 0xff;
o3 = bits & 0xff;
if (h3 == 64) {
tmp_arr[ac++] = String.fromCharCode(o1);
} else if (h4 == 64) {
tmp_arr[ac++] = String.fromCharCode(o1, o2);
} else {
tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
}
} while (i < l);
return tmp_arr.join('');
}
}
var displayInTextArea = function(output, label) {
var $l = $('<label for="'+label+'">'+label+'</label>').appendTo(document.body)
$('<textarea id="'+label+'"></textarea>').appendTo($l).text(btoa(output))
}
var datestringregex = /\/CreationDate \(D:\d+\)/
, replacementdatestring = '/CreationDate (D:0)'
, producerstringregex = /\/Producer\s+\(jsPDF\s+\d+\)/
, replacementproducerstring = '/Producer (jsPDF 0)'
, removeMinorDiffs = function(t){
t = t.replace(datestringregex, replacementdatestring)
t = t.replace(producerstringregex, replacementproducerstring)
if (t.trim)
return t.trim()
else
return t
}
, testinventory = {
"001_blankpdf.pdf": function(){
var doc = new jsPDF()
return doc.output()
}
, "002_twopagedoc.pdf":function(){
var doc = new jsPDF()
doc.text('Hello world!', 20, 20)
doc.text('This is client-side Javascript, pumping out a PDF.', 20, 30)
doc.addPage()
doc.text('Do you like that?', 20, 20)
return doc.output()
}
, "002_twopagedoc_oldapi.pdf":function(){
var doc = new jsPDF()
doc.text(20, 20, 'Hello world!')
doc.text(20, 30, 'This is client-side Javascript, pumping out a PDF.')
doc.addPage()
doc.text(20, 20, 'Do you like that?')
return doc.output()
}
, "003_demolandscape.pdf":function(){
var doc = new jsPDF('landscape')
doc.text('Hello landscape world!', 20, 20)
return doc.output()
}
, "004_fontsizes.pdf":function(){
var doc = new jsPDF()
doc.setFontSize(22)
doc.text(20, 20, 'This is a title')
doc.setFontSize(16)
doc.text(20, 30, 'This is some normal sized text underneath.');
return doc.output()
}
, "005_demofonttypes.pdf":function(){
var doc = new jsPDF()
doc.text(20, 20, 'This is the default font.')
doc.setFont("courier")
doc.setFontType("normal")
doc.text(20, 30, 'This is courier normal.')
doc.setFont("times")
doc.setFontType("italic")
doc.text(20, 40, 'This is times italic.')
doc.setFont("helvetica")
doc.setFontType("bold")
doc.text(20, 50, 'This is helvetica bold.')
doc.setFont("courier")
doc.setFontType("bolditalic")
doc.text(20, 60, 'This is courier bolditalic.')
return doc.output()
}
, "006_demotestcolors.pdf":function(){
var doc = new jsPDF()
doc.setTextColor(100)
doc.text(20, 20, 'This is gray.')
doc.setTextColor(150)
doc.text(20, 30, 'This is light gray.')
doc.setTextColor(255,0,0)
doc.text(20, 40, 'This is red.')
doc.setTextColor(0,255,0)
doc.text(20, 50, 'This is green.')
doc.setTextColor(0,0,255)
doc.text(20, 60, 'This is blue.')
return doc.output()
}
, "007_demometadata.pdf":function(){
var doc = new jsPDF()
doc.text(20, 20, 'This PDF has a title, subject, author, keywords and a creator.')
// Optional - set properties on the document
doc.setProperties({
title: 'Title',
subject: 'This is the subject',
author: 'James Hall',
keywords: 'generated, javascript, web 2.0, ajax',
creator: 'MEEE'
})
return doc.output()
}
, "008_demorectangles.pdf":function(){
var doc = new jsPDF()
doc.rect(20, 20, 10, 10); // empty square
doc.rect(40, 20, 10, 10, 'F') // filled square
doc.setDrawColor(255,0,0)
doc.rect(60, 20, 10, 10); // empty red square
doc.setDrawColor(255,0,0)
doc.rect(80, 20, 10, 10, 'FD') // filled square with red borders
doc.setDrawColor(0)
doc.setFillColor(255,0,0)
doc.rect(100, 20, 10, 10, 'F') // filled red square
doc.setDrawColor(0)
doc.setFillColor(255,0,0)
doc.rect(120, 20, 10, 10, 'FD') // filled red square with black borders
return doc.output()
}
, "009_demoliness.pdf":function(){
var doc = new jsPDF()
doc.line(20, 20, 60, 20) // horizontal line
doc.setLineWidth(0.5)
doc.line(20, 25, 60, 25)
doc.setLineWidth(1)
doc.line(20, 30, 60, 30)
doc.setLineWidth(1.5)
doc.line(20, 35, 60, 35)
doc.setDrawColor(255,0,0) // draw red lines
doc.setLineWidth(0.1)
doc.line(100, 20, 100, 60) // vertical line
doc.setLineWidth(0.5)
doc.line(105, 20, 105, 60)
doc.setLineWidth(1)
doc.line(110, 20, 110, 60)
doc.setLineWidth(1.5)
doc.line(115, 20, 115, 60)
return doc.output()
}
, "010_democircles.pdf":function(){
var doc = new jsPDF()
doc.ellipse(40, 20, 10, 5)
doc.setFillColor(0,0,255)
doc.ellipse(80, 20, 10, 5, 'F')
doc.setLineWidth(1)
doc.setDrawColor(0)
doc.setFillColor(255,0,0)
doc.circle(120, 20, 5, 'FD')
return doc.output()
}
, "011_multilinetext.pdf":function(){
var doc = new jsPDF()
, text = [
'This is line one'
, 'This is line two'
, 'This is line three'
, 'This is line four'
, 'This is line five'
]
doc.text(text, 20, 20)
return doc.output()
}
, "012_multiplelines.pdf":function(){
var doc = new jsPDF()
, x1 = 40
, y1 = 40
, lines = [
[10,10]
, [-20,10]
, [-15,5,-20,10,-30,15]
]
doc.lines(lines, x1, y1)
doc.lines(lines, x1, y1, [-1, -1])
doc.lines(lines, x1, y1, [0.5, -0.5])
doc.lines(lines, x1, y1, [-2, 2])
return doc.output()
}
}
, testrunner = function(reference_file_name, test_data_yielder){
asyncTest(reference_file_name, function() {
//QUnit.stop()
require(['text!'+reference_file_name])
.then(function(expectedtext){
QUnit.expect(1)
var output = test_data_yielder()
if (dump) {
displayInTextArea(output, reference_file_name)
QUnit.equal(true, true)
} else {
QUnit.equal(
removeMinorDiffs( output )
, removeMinorDiffs( expectedtext )
)
}
QUnit.start()
//stop()
})
})
}
////////////////////////////////////////////////////
// running homogenous tests
for (var filename in testinventory){
if (testinventory.hasOwnProperty(filename)){
testrunner(
filename
, testinventory[filename]
)
}
}
asyncTest('013_sillysvgrenderer', function() {
//QUnit.stop()
require(['text!013_sillysvgrenderer.svg', 'text!013_sillysvgrenderer.pdf'])
.then(function(svgtext, expectedtext){
QUnit.expect(1)
var pdf = jsPDF() // 'p','pt','letter')
pdf.addSVG(svgtext, 20, 20, pdf.internal.pageSize.width - 20*2)
// pdf.output('dataurl')
// window.mypdf = pdf.output('dataurlstring')
var output = pdf.output()
if (dump) {
displayInTextArea(output, '013_sillysvgrenderer')
QUnit.equal(true, true)
} else {
QUnit.equal(
removeMinorDiffs( output )
, removeMinorDiffs( expectedtext )
)
}
QUnit.start()
//stop()
})
})
// handcrafted tests
asyncTest('014_addImage', function() {
//QUnit.stop()
require(
['text!014_addImage.jpeg.base64.txt', 'text!014_addImage.pdf.base64.txt']
).then(function(base64encodedJpeg, base64encodedPDF){
QUnit.expect(2)
var pdf = jsPDF()
pdf.addImage(atob(base64encodedJpeg), 'jpeg', 20, 20)
QUnit.equal(
// just testing if it does not blow up.
pdf.output('datauristring') !== ''
, true
)
pdf = jsPDF()
pdf.addImage(atob(base64encodedJpeg), 'jpeg', 20, 20)
// window.pdfbase64 = btoa(pdf.output())
var output = pdf.output()
if (dump) {
displayInTextArea(output, '014_addImage')
QUnit.equal(true, true)
} else {
QUnit.equal(
removeMinorDiffs( output )
, removeMinorDiffs( atob(base64encodedPDF) )
)
}
QUnit.start()
//stop()
})
})
// handcrafted tests
asyncTest('015_splittext', function() {
//QUnit.stop()
require(
['015_splittext', 'text!015_splittext.pdf']
).then(function(runner, shouldbe){
QUnit.expect(1)
var pdf = runner(jsPDF)
var output = pdf.output()
if (dump) {
displayInTextArea(output, '015_splittext')
QUnit.equal(true, true)
} else {
QUnit.equal(
removeMinorDiffs( output )
, removeMinorDiffs( shouldbe )
)
}
QUnit.start()
//stop()
})
})
}) // end of document.ready(