/
jspdf-tests.ts
640 lines (576 loc) · 16 KB
/
jspdf-tests.ts
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/* eslint-disable @typescript-eslint/no-unused-vars */
import {
jsPDF,
GState,
TilingPattern,
ShadingPattern,
AcroFormPasswordField,
AcroFormChoiceField,
AcroFormButton,
AcroFormComboBox,
AcroFormCheckBox,
AcroFormRadioButton,
AcroFormEditBox,
AcroFormPushButton,
AcroFormListBox,
AcroFormTextField
} from "jspdf";
function pubsub() {
const doc = new jsPDF();
const token = doc.internal.events.subscribe("topic", (a, b) => {}, true);
doc.internal.events.unsubscribe(token);
doc.internal.events.publish("topic", 1, "foo");
const topics = doc.internal.events.getTopics();
if (topics["topic"][token][1]) {
topics["topic"][token][0](1, "foo");
}
}
function classes() {
new GState({});
new TilingPattern([], 0, 0);
new ShadingPattern("axial", [], []);
new AcroFormChoiceField();
new AcroFormListBox();
new AcroFormComboBox();
new AcroFormEditBox();
new AcroFormButton();
new AcroFormPushButton();
new AcroFormRadioButton();
new AcroFormCheckBox();
new AcroFormTextField();
new AcroFormPasswordField();
}
function test_simple_two_page_document() {
const doc = new jsPDF();
doc.text("Hello world!", 20, 20);
doc.text("This is client-side Javascript, pumping out a PDF.", 20, 20);
doc.addPage();
doc.text("Do you like that?", 20, 20);
doc.save("Test.pdf");
}
function test_add_pages_with_different_format() {
const doc = new jsPDF();
doc.text("Hello world!", 20, 20);
doc.addPage("a5", "l");
doc.text("Do you like that?", 20, 20);
doc.addPage("c6");
doc.text("Do you like that?", 20, 20);
doc.addPage([595.28, 841.89]);
doc.text("Do you like that?", 20, 20);
doc.save("Test.pdf");
}
function test_landscape() {
const doc = new jsPDF("landscape");
doc.text("Hello landscape world!", 20, 20);
doc.save("Test.pdf");
}
function test_metadata() {
const doc = new jsPDF();
doc.text(
"This PDF has a title, subject, author, keywords and a creator.",
20,
20
);
doc.setProperties({
title: "Title",
subject: "This is the subject",
author: "James Hall",
keywords: "generated, javascript, web 2.0, ajax",
creator: "MEEE"
});
doc.save("Test.pdf");
}
function test_user_input() {
const doc = new jsPDF();
doc.text(
"This PDF has a title, subject, author, keywords and a creator.",
20,
20
);
doc.setProperties({
title: "Title",
subject: "This is the subject",
author: "James Hall",
keywords: "generated, javascript, web 2.0, ajax",
creator: "MEEE"
});
doc.save("Test.pdf");
}
function test_font_sizes() {
const doc = new jsPDF();
doc.setFontSize(22);
doc.text("This is a title", 20, 20);
doc.setFontSize(16);
doc.text("This is some normal sized text underneath.", 20, 30);
doc.save("Test.pdf");
}
function test_font() {
const doc = new jsPDF();
doc.text("This is the default font.", 20, 20);
doc.setFont("courier");
}
function test_text_colors() {
const doc = new jsPDF();
doc.setTextColor(100);
doc.setTextColor(150);
doc.setTextColor(255, 0, 0);
doc.setTextColor(0, 255, 0);
doc.setTextColor(0, 0, 255);
doc.setTextColor("red");
doc.save("Test.pdf");
}
function test_font_metrics_based_line_sizing_split() {
const pdf = new jsPDF("p", "in", "letter");
const sizes: number[] = [12, 16, 20];
const fonts = [
["Times", "Roman"],
["Helvetica", ""],
["Times", "Italic"]
];
let font: string[];
let size: number;
let lines: any[];
let verticalOffset = 0.5; // inches on a 8.5 x 11 inch sheet.
const loremipsum = "Lorem ipsum dolor sit amet, ...";
for (const i in fonts) {
if (fonts.hasOwnProperty(i)) {
font = fonts[i];
size = sizes[i];
lines = pdf
.setFont(font[0], font[1])
.setFontSize(size)
.splitTextToSize(loremipsum, 7.5);
pdf.text(lines, 0.5, verticalOffset + size / 72);
verticalOffset += ((lines.length + 0.5) * size) / 72;
}
}
pdf.save("Test.pdf");
}
function test_rect_squares() {
const 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
doc.setDrawColor(0);
doc.setFillColor(255, 255, 255);
doc.roundedRect(140, 20, 10, 10, 3, 3, "FD"); // Black sqaure with rounded corners
doc.save("Test.pdf");
}
function test_lines() {
const 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);
doc.save("Test.pdf");
}
function test_circles_ellipses() {
const 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");
doc.save("Test.pdf");
}
function test_triangles() {
const doc = new jsPDF();
doc.triangle(60, 100, 60, 120, 80, 110, "FD");
doc.setLineWidth(1);
doc.setDrawColor(255, 0, 0);
doc.setFillColor(0, 0, 255);
doc.triangle(100, 100, 110, 100, 120, 130, "FD");
doc.save("My file.pdf");
}
function test_images() {
const getImageFromUrl = function(url: string, callback: Function) {
const img = new Image();
img.onerror = function() {
alert('Cannot load image: "' + url + '"');
};
img.onload = function() {
callback(img);
};
img.src = url;
};
const createPDF = function(imgData: string) {
const doc = new jsPDF();
doc.addImage(imgData, "JPEG", 10, 10, 50, 50, "monkey"); // Cache the image using the alias 'monkey'
doc.addImage("monkey", 70, 10, 100, 120); // use the cached 'monkey' image, JPEG is optional regardless
doc.addImage({
imageData: imgData,
rotation: -20,
x: 10,
y: 78,
width: 45,
height: 58
});
doc.output("datauri", { filename: "test.pdf" });
};
getImageFromUrl("thinking-monkey.jpg", createPDF);
}
function test_context2d_smiley() {
const doc = new jsPDF("p", "pt", "a4");
const ctx = doc.context2d;
ctx.beginPath();
ctx.arc(75, 75, 50, 0, Math.PI * 2, true); // Outer circle
ctx.moveTo(110, 75);
ctx.arc(75, 75, 35, 0, Math.PI, false); // Mund
ctx.moveTo(65, 65);
ctx.arc(60, 65, 5, 0, Math.PI * 2, true); // Linkes Auge
ctx.moveTo(95, 65);
ctx.arc(90, 65, 5, 0, Math.PI * 2, true); // Rechtes Auge
ctx.stroke();
}
function test_context2d_warnsign() {
const doc = new jsPDF("p", "pt", "a4");
const context = doc.context2d;
const primaryColor = "#ffc821";
const secondaryColor = "black";
const tertiaryColor = "black";
const lineWidth = 10;
// Dimensions of the triangle
const width = 125;
const height = 100;
const padding = 20;
// Create a triangluar path
context.beginPath();
context.moveTo(padding + width / 2, padding);
context.lineTo(padding + width, height + padding);
context.lineTo(padding, height + padding);
context.closePath();
// Create fill gradient
let gradient = context.createLinearGradient(0, 0, 0, height);
gradient.addColorStop(0, primaryColor);
gradient.addColorStop(1, secondaryColor);
// Add a shadow around the object
context.shadowBlur = 10;
context.shadowColor = "black";
// Stroke the outer outline
context.lineWidth = lineWidth * 2;
context.lineJoin = "round";
context.strokeStyle = gradient;
context.stroke();
// Turn off the shadow, or all future fills will have shadows
context.shadowColor = "transparent";
// Fill the path
context.fillStyle = gradient;
context.fill();
// Add a horizon reflection with a gradient to transparent
gradient = context.createLinearGradient(0, padding, 0, padding + height);
gradient.addColorStop(0, "transparent");
gradient.addColorStop(0.5, "transparent");
gradient.addColorStop(0.5, tertiaryColor);
gradient.addColorStop(1, secondaryColor);
context.fillStyle = gradient;
context.fill();
// Stroke the inner outline
context.lineWidth = lineWidth;
context.lineJoin = "round";
context.strokeStyle = "#333";
context.stroke();
// Draw the text exclamation point
context.textAlign = "center";
context.textBaseline = "middle";
context.font = "bold 60px 'Times New Roman', Times, serif";
context.fillStyle = "#333";
context.fillText("!", padding + width / 2, padding + height / 1.5);
}
function test_context2d_fields() {
const doc = new jsPDF();
const ctx = doc.canvas.getContext("2d");
ctx.fillStyle = "#000000";
ctx.filter = "none";
ctx.font = "10px sans-serif";
ctx.globalAlpha = 1;
ctx.globalCompositeOperation = "source-over";
ctx.imageSmoothingEnabled = true;
ctx.imageSmoothingQuality = "low";
ctx.lineCap = "butt";
ctx.lineDashOffset = 0;
ctx.lineJoin = "miter";
ctx.lineWidth = 1;
ctx.miterLimit = 10;
ctx.shadowBlur = 0;
ctx.shadowColor = "rgba(0, 0, 0, 0)";
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
ctx.strokeStyle = "#000000";
ctx.textAlign = "start";
ctx.textBaseline = "alphabetic";
}
function test_context2d_functions() {
const doc = new jsPDF();
doc.context2d.moveTo(1, 1);
doc.context2d.lineTo(1, 1);
doc.context2d.quadraticCurveTo(1, 1, 1, 1);
doc.context2d.bezierCurveTo(1, 1, 1, 1, 1, 1);
doc.context2d.arc(1, 1, 1, 1, 1, false);
doc.context2d.rect(1, 1, 1, 1);
doc.context2d.fillRect(1, 1, 1, 1);
doc.context2d.strokeRect(1, 1, 1, 1);
doc.context2d.clearRect(1, 1, 1, 1);
doc.context2d.fillText("valid", 1, 1, 1);
doc.context2d.strokeText("valid", 1, 1, 1);
doc.context2d.measureText("valid");
doc.context2d.scale(1, 1);
doc.context2d.rotate(1);
doc.context2d.translate(1, 1);
doc.context2d.transform(1, 1, 1, 1, 1, 1);
}
function test_add_font() {
const doc = new jsPDF("p", "pt", "a4");
doc.addFont("helvetica", "helvetica", "normal", "StandardEncoding");
}
function test_vfs() {
const doc = new jsPDF("p", "pt", "a4");
doc.addFileToVFS("test.pdf", "BADFACE");
doc.getFileFromVFS("test.pdf");
doc.existsFileInVFS("test.pdf");
}
function test_outline() {
const doc = new jsPDF({ unit: "pt" });
doc.outline.add(null, "Page 1", { pageNumber: 1 });
doc.addPage();
}
function test_page_operations() {
const doc = new jsPDF();
doc.text("Text that will end up on page 2", 20, 20);
doc.addPage();
doc.text("Text that will end up on page 1", 20, 20);
doc.movePage(2, 1);
doc.addPage();
doc.text("Text that will end up on page 3", 20, 20);
doc.deletePage(3);
}
function test_displayMode() {
const doc = new jsPDF();
doc.setDisplayMode("fullheight");
doc.setDisplayMode("fullwidth");
doc.setDisplayMode("fullpage");
doc.setDisplayMode("original");
doc.setDisplayMode("300%");
doc.setDisplayMode(2);
doc.setDisplayMode(null, "continuous");
doc.setDisplayMode(null, "single");
doc.setDisplayMode(null, "twoleft");
doc.setDisplayMode(null, "two");
doc.setDisplayMode(null, "tworight");
doc.setDisplayMode(null, null, "UseOutlines");
doc.setDisplayMode(null, null, "UseThumbs");
doc.setDisplayMode(null, null, "FullScreen");
}
function test_put_total_pages() {
const doc = new jsPDF();
const totalPagesExp = "{totalPages}";
doc.text("Page 1 of {totalPages}", 10, 10);
doc.addPage();
doc.text("Page 2 of {totalPages}", 10, 10);
if (typeof doc.putTotalPages === "function") {
doc.putTotalPages(totalPagesExp);
}
}
function test_autoprint() {
const doc = new jsPDF();
doc.text("This is a test", 10, 10);
doc.autoPrint();
doc.autoPrint({ variant: "javascript" });
}
function test_viewerpreferences() {
const doc = new jsPDF();
doc.text("This is a test", 10, 10);
doc.viewerPreferences({ HideToolbar: true });
doc.viewerPreferences({ HideMenubar: true });
doc.viewerPreferences({ HideWindowUI: true });
doc.viewerPreferences({ NumCopies: 9 });
doc.viewerPreferences({ HideWindowUI: true });
doc.viewerPreferences({ FitWindow: true }, true);
doc.viewerPreferences({ ViewArea: "MediaBox" });
doc.viewerPreferences({
PrintPageRange: [
[1, 3],
[5, 9]
]
});
doc.viewerPreferences({ HideWindowUI: true });
doc.viewerPreferences("reset");
doc.viewerPreferences({ FitWindow: true });
}
function test_arabic() {
const doc = new jsPDF();
doc.processArabic("ددد");
}
function test_split_text_to_size() {
const doc = new jsPDF();
doc.setFont("Courier");
doc.getTextWidth("Lorem Ipsum");
doc.getStringUnitWidth("Lorem Ipsum");
doc.getCharWidthsArray("Lorem Ipsum");
doc.splitTextToSize(
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.",
100
);
}
function test_setlanguage() {
const doc = new jsPDF();
doc.setLanguage("en-US");
doc.setLanguage("de-DE");
}
function test_annotations() {
const doc = new jsPDF();
doc.createAnnotation({
type: "text",
title: "note",
bounds: {
x: 10,
y: 10,
w: 200,
h: 80
},
contents: "This is text annotation (closed by default)",
open: false
});
doc.createAnnotation({
type: "text",
title: "note",
bounds: {
x: 10,
y: 10,
w: 200,
h: 80
},
contents: "This is text annotation (open by default)",
open: true
});
doc.createAnnotation({
type: "freetext",
bounds: {
x: 0,
y: 10,
w: 200,
h: 20
},
contents: "This is a freetext annotation",
color: "#ff0000"
});
const pdf = new jsPDF();
const x = 1,
y = 1,
i = 1;
var width = pdf.textWithLink(" [100%]", x, y, {
pageNumber: i,
magFactor: "XYZ",
zoom: 1
});
var width = pdf.textWithLink(" [200%]", x, y, {
pageNumber: i,
magFactor: "XYZ",
zoom: 2
});
var width = pdf.textWithLink(" [50%]", x, y, {
pageNumber: i,
magFactor: "XYZ",
zoom: 0.5
});
var width = pdf.textWithLink(" [Fit]", x, y, {
pageNumber: i,
magFactor: "Fit"
});
var width = pdf.textWithLink(" [FitH]", x, y, {
pageNumber: i,
magFactor: "FitH"
});
var width = pdf.textWithLink(" [FitV]", x, y, {
pageNumber: i,
magFactor: "FitV"
});
}
function test_AcroForm() {
const doc = new jsPDF();
const checkBox = doc.AcroForm.CheckBox();
checkBox.value = "Off";
const radioGroup = doc.AcroForm.RadioButton();
radioGroup.createOption("Test");
const textField = doc.AcroForm.TextField();
textField.value = "Test";
textField.defaultValue = "";
}
function test_html() {
const doc = new jsPDF();
doc.html(document.body, {
callback: function(doc) {},
html2canvas: {
allowTaint: false
},
jsPDF: doc
});
}
function test_addImage() {
const doc = new jsPDF();
doc.addImage({
imageData: "/image.png",
x: 0,
y: 0,
width: 100,
height: 100,
compression: "FAST"
});
}
function test_loadFile() {
const doc = new jsPDF();
doc.loadFile("../image.png");
doc.loadFile("../image.png", false, function(data) {
return data;
});
}
function test_simpleTwoPageDocumentWithEncryption() {
const doc = new jsPDF({
encryption: {
userPassword: "longpassword",
userPermissions: ["print", "copy"]
}
});
doc.text("Hello world!", 20, 20);
doc.text("This is client-side Javascript, pumping out a PDF.", 20, 20);
doc.addPage();
doc.text("Do you like that?", 20, 20);
doc.save("Test.pdf");
}
function test_addImageWithEncryption() {
const doc = new jsPDF({
encryption: {
userPassword: "password",
ownerPassword: "password"
}
});
doc.addImage({
imageData: "/image.png",
x: 0,
y: 0,
width: 100,
height: 100
});
}