forked from simonbengtsson/jsPDF-AutoTable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.js
More file actions
633 lines (564 loc) · 16.9 KB
/
examples.js
File metadata and controls
633 lines (564 loc) · 16.9 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
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
/*
|--------------------------------------------------------------------------
| This file contains examples of how to use this plugin
|--------------------------------------------------------------------------
|
| To see what the documents generated by these examples looks like you can open
| ´examples.html´ or go to http://simonbengtsson.github.io/jsPDF-AutoTable.
|
| To make it possible to view each example in examples.html some extra code
| is added to the examples below. For example they return their jspdf
| doc instance and gets generated data from the library faker.js. See simple.html
| for a minimal example.
*/
var faker = window.faker
var examples = {}
window.examples = examples
// Basic - shows what a default table looks like
examples.basic = function () {
var doc = new jsPDF()
// From HTML
doc.autoTable({ html: '.table' })
// From Javascript
var finalY = doc.lastAutoTable.finalY || 10
doc.text('From javascript arrays', 14, finalY + 15)
doc.autoTable({
startY: finalY + 20,
head: [['ID', 'Name', 'Email', 'Country', 'IP-address']],
body: [
['1', 'Donna', 'dmoore0@furl.net', 'China', '211.56.242.221'],
['2', 'Janice', 'jhenry1@theatlantic.com', 'Ukraine', '38.36.7.199'],
[
'3',
'Ruth',
'rwells2@constantcontact.com',
'Trinidad and Tobago',
'19.162.133.184',
],
['4', 'Jason', 'jray3@psu.edu', 'Brazil', '10.68.11.42'],
['5', 'Jane', 'jstephens4@go.com', 'United States', '47.32.129.71'],
['6', 'Adam', 'anichols5@com.com', 'Canada', '18.186.38.37'],
],
})
finalY = doc.lastAutoTable.finalY
doc.text('From HTML with CSS', 14, finalY + 15)
doc.autoTable({
startY: finalY + 20,
html: '.table',
useCss: true,
})
return doc
}
// Minimal - shows how compact tables can be drawn
examples.minimal = function () {
var doc = new jsPDF()
doc.autoTable({
html: '.table',
tableWidth: 'wrap',
styles: { cellPadding: 0.5, fontSize: 8 },
})
return doc
}
// Long data - shows how the overflow features looks and can be used
examples.long = function () {
var doc = new jsPDF('l')
var head = headRows()
head[0]['text'] = 'Text'
var body = bodyRows(4)
body.forEach(function (row) {
row['text'] = faker.lorem.sentence(100)
})
doc.text("Overflow 'ellipsize' with one column with long content", 14, 20)
doc.autoTable({
head: head,
body: body,
startY: 25,
// Default for all columns
styles: { overflow: 'ellipsize', cellWidth: 'wrap' },
// Override the default above for the text column
columnStyles: { text: { cellWidth: 'auto' } },
})
doc.text(
"Overflow 'linebreak' (default) with one column with long content",
14,
doc.lastAutoTable.finalY + 10
)
doc.autoTable({
head: head,
body: body,
startY: doc.lastAutoTable.finalY + 15,
rowPageBreak: 'auto',
bodyStyles: { valign: 'top' },
})
return doc
}
// Content - shows how tables can be integrated with any other pdf content
examples.content = function () {
var doc = new jsPDF()
doc.setFontSize(18)
doc.text('With content', 14, 22)
doc.setFontSize(11)
doc.setTextColor(100)
// jsPDF 1.4+ uses getWidth, <1.4 uses .width
var pageSize = doc.internal.pageSize
var pageWidth = pageSize.width ? pageSize.width : pageSize.getWidth()
var text = doc.splitTextToSize(faker.lorem.sentence(45), pageWidth - 35, {})
doc.text(text, 14, 30)
doc.autoTable({
head: headRows(),
body: bodyRows(40),
startY: 50,
showHead: 'firstPage',
})
doc.text(text, 14, doc.lastAutoTable.finalY + 10)
return doc
}
// Multiple - shows how multiple tables can be drawn both horizontally and vertically
examples.multiple = function () {
var doc = new jsPDF()
doc.text('Multiple tables', 14, 20)
doc.autoTable({ startY: 30, head: headRows(), body: bodyRows(25) })
var pageNumber = doc.internal.getNumberOfPages()
doc.autoTable({
columns: [
{ dataKey: 'id', header: 'ID' },
{ dataKey: 'name', header: 'Name' },
{ dataKey: 'expenses', header: 'Sum' },
],
body: bodyRows(15),
startY: 240,
showHead: 'firstPage',
styles: { overflow: 'hidden' },
margin: { right: 107 },
})
doc.setPage(pageNumber)
doc.autoTable({
columns: [
{ dataKey: 'id', header: 'ID' },
{ dataKey: 'name', header: 'Name' },
{ dataKey: 'expenses', header: 'Sum' },
],
body: bodyRows(15),
startY: 240,
showHead: 'firstPage',
styles: { overflow: 'hidden' },
margin: { left: 107 },
})
for (var j = 0; j < 3; j++) {
doc.autoTable({
head: headRows(),
body: bodyRows(),
startY: doc.lastAutoTable.finalY + 10,
pageBreak: 'avoid',
})
}
return doc
}
// Header and footers - shows how header and footers can be drawn
examples['header-footer'] = function () {
var doc = new jsPDF()
var totalPagesExp = '{total_pages_count_string}'
doc.autoTable({
head: headRows(),
body: bodyRows(40),
didDrawPage: function (data) {
// Header
doc.setFontSize(20)
doc.setTextColor(40)
if (base64Img) {
doc.addImage(base64Img, 'JPEG', data.settings.margin.left, 15, 10, 10)
}
doc.text('Report', data.settings.margin.left + 15, 22)
// Footer
var str = 'Page ' + doc.internal.getNumberOfPages()
// Total page number plugin only available in jspdf v1.0+
if (typeof doc.putTotalPages === 'function') {
str = str + ' of ' + totalPagesExp
}
doc.setFontSize(10)
// jsPDF 1.4+ uses getWidth, <1.4 uses .width
var pageSize = doc.internal.pageSize
var pageHeight = pageSize.height ? pageSize.height : pageSize.getHeight()
doc.text(str, data.settings.margin.left, pageHeight - 10)
},
margin: { top: 30 },
})
// Total page number plugin only available in jspdf v1.0+
if (typeof doc.putTotalPages === 'function') {
doc.putTotalPages(totalPagesExp)
}
return doc
}
// Minimal - shows how compact tables can be drawn
examples.defaults = function () {
// Global defaults
// (would apply to all documents if more than one were created)
jsPDF.autoTableSetDefaults({
headStyles: { fillColor: 0 },
})
var doc = new jsPDF()
doc.text('Global options (black header)', 15, 20)
doc.autoTable({ head: headRows(), body: bodyRows(5), startY: 25 })
// Document defaults
jsPDF.autoTableSetDefaults(
{
headStyles: { fillColor: [155, 89, 182] }, // Purple
didDrawPage: function (data) {
var finalY = doc.lastAutoTable.finalY + 15
var leftMargin = data.settings.margin.left
doc.text('Default options (purple header)', leftMargin, finalY)
},
},
doc
)
var startY = doc.lastAutoTable.finalY + 20
doc.autoTable({ head: headRows(), body: bodyRows(5), startY: startY })
// Reset defaults
doc.autoTableSetDefaults(null)
jsPDF.autoTableSetDefaults(null)
var finalY = doc.lastAutoTable.finalY
doc.text('After reset (blue header)', 15, finalY + 15)
doc.autoTable({ head: headRows(), body: bodyRows(5), startY: finalY + 20 })
return doc
}
// Column styles - shows how tables can be drawn with specific column styles
examples.colstyles = function () {
var doc = new jsPDF()
doc.autoTable({
head: headRows(),
body: bodyRows(),
showHead: false,
// Note that the "id" key below is the same as the column's dataKey used for
// the head and body rows. If your data is entered in array form instead you have to
// use the integer index instead i.e. `columnStyles: {5: {fillColor: [41, 128, 185], ...}}`
columnStyles: {
id: { fillColor: [41, 128, 185], textColor: 255, fontStyle: 'bold' },
},
})
return doc
}
// Col spans and row spans
examples.spans = function () {
var doc = new jsPDF('p', 'pt')
doc.text('Rowspan and colspan', 40, 50)
var raw = bodyRows(40)
var body = []
for (var i = 0; i < raw.length; i++) {
var row = []
for (var key in raw[i]) {
row.push(raw[i][key])
}
if (i % 5 === 0) {
row.unshift({
rowSpan: 5,
content: i / 5 + 1,
styles: { valign: 'middle', halign: 'center' },
})
}
body.push(row)
}
doc.autoTable({
startY: 60,
head: [
[
{
content: 'People',
colSpan: 5,
styles: { halign: 'center', fillColor: [22, 160, 133] },
},
],
],
body: body,
theme: 'grid',
})
return doc
}
// Themes - shows how the different themes looks
examples.themes = function () {
var doc = new jsPDF()
doc.text('Theme "striped"', 14, 16)
doc.autoTable({ head: headRows(), body: bodyRows(5), startY: 20 })
doc.text('Theme "grid"', 14, doc.lastAutoTable.finalY + 10)
doc.autoTable({
head: headRows(),
body: bodyRows(5),
startY: doc.lastAutoTable.finalY + 14,
theme: 'grid',
})
doc.text('Theme "plain"', 14, doc.lastAutoTable.finalY + 10)
doc.autoTable({
head: headRows(),
body: bodyRows(5),
startY: doc.lastAutoTable.finalY + 14,
theme: 'plain',
})
return doc
}
// Nested tables
examples.nested = function () {
var doc = new jsPDF()
doc.text('Nested tables', 14, 16)
var nestedTableHeight = 100
var nestedTableCell = {
content: '',
// Dynamic height of nested tables are not supported right now
// so we need to define height of the parent cell
styles: { minCellHeight: 100 },
}
doc.autoTable({
theme: 'grid',
head: [['2019', '2020']],
body: [[nestedTableCell]],
foot: [['2019', '2020']],
startY: 20,
didDrawCell: function (data) {
if (data.row.index === 0 && data.row.section === 'body') {
doc.autoTable({
startY: data.cell.y + 2,
margin: { left: data.cell.x + 2 },
tableWidth: data.cell.width - 4,
styles: {
maxCellHeight: 4,
},
columns: [
{ dataKey: 'id', header: 'ID' },
{ dataKey: 'name', header: 'Name' },
{ dataKey: 'expenses', header: 'Sum' },
],
body: bodyRows(),
})
}
},
})
return doc
}
// Custom style - shows how custom styles can be applied
examples.custom = function () {
var doc = new jsPDF()
doc.autoTable({
head: headRows(),
body: bodyRows(),
foot: headRows(),
margin: { top: 37 },
tableLineColor: [231, 76, 60],
tableLineWidth: 1,
styles: {
lineColor: [44, 62, 80],
lineWidth: 1,
},
headStyles: {
fillColor: [241, 196, 15],
fontSize: 15,
},
footStyles: {
fillColor: [241, 196, 15],
fontSize: 15,
},
bodyStyles: {
fillColor: [52, 73, 94],
textColor: 240,
},
alternateRowStyles: {
fillColor: [74, 96, 117],
},
// Note that the "email" key below is the same as the column's dataKey used for
// the head and body rows. If your data is entered in array form instead you have to
// use the integer index instead i.e. `columnStyles: {5: {fillColor: [41, 128, 185], ...}}`
columnStyles: {
email: {
fontStyle: 'bold',
},
city: {
// The font file mitubachi-normal.js is included on the page and was created from mitubachi.ttf
// with https://rawgit.com/MrRio/jsPDF/master/fontconverter/fontconverter.html
// refer to https://github.com/MrRio/jsPDF#use-of-utf-8--ttf
font: 'mitubachi',
},
id: {
halign: 'right',
},
},
allSectionHooks: true,
// Use for customizing texts or styles of specific cells after they have been formatted by this plugin.
// This hook is called just before the column width and other features are computed.
didParseCell: function (data) {
if (data.row.index === 5) {
data.cell.styles.fillColor = [40, 170, 100]
}
if (
(data.row.section === 'head' || data.row.section === 'foot') &&
data.column.dataKey === 'expenses'
) {
data.cell.text = '' // Use an icon in didDrawCell instead
}
if (data.column.dataKey === 'city') {
data.cell.styles.font = 'mitubachi'
if (data.row.section === 'head') {
data.cell.text = 'シティ'
}
if (data.row.index === 0 && data.row.section === 'body') {
data.cell.text = 'とうきょう'
}
}
},
// Use for changing styles with jspdf functions or customize the positioning of cells or cell text
// just before they are drawn to the page.
willDrawCell: function (data) {
if (data.row.section === 'body' && data.column.dataKey === 'expenses') {
if (data.cell.raw > 750) {
doc.setTextColor(231, 76, 60) // Red
}
}
},
// Use for adding content to the cells after they are drawn. This could be images or links.
// You can also use this to draw other custom jspdf content to cells with doc.text or doc.rect
// for example.
didDrawCell: function (data) {
if (
(data.row.section === 'head' || data.row.section === 'foot') &&
data.column.dataKey === 'expenses' &&
coinBase64Img
) {
doc.addImage(
coinBase64Img,
'PNG',
data.cell.x + 5,
data.cell.y + 2,
5,
5
)
}
},
// Use this to add content to each page that has the autoTable on it. This can be page headers,
// page footers and page numbers for example.
didDrawPage: function (data) {
doc.setFontSize(18)
doc.text('Custom styling with hooks', data.settings.margin.left, 22)
doc.setFontSize(12)
doc.text(
'Conditional styling of cells, rows and columns, cell and table borders, custom font, image in cell',
data.settings.margin.left,
30
)
},
})
return doc
}
// Split columns - shows how the overflowed columns split into pages
examples.horizontalPageBreak = function () {
var doc = new jsPDF('l')
var head = headRows()
head[0].region = 'Region'
head[0].country = 'Country'
head[0].zipcode = 'Zipcode'
head[0].phone = 'Phone'
// head[0].timeZone = 'Timezone';
head[0]['text'] = 'Text'
var body = bodyRows(4)
body.forEach(function (row) {
row['text'] = faker.lorem.sentence(100)
row['zipcode'] = faker.address.zipCode()
row['country'] = faker.address.country()
row['region'] = faker.address.state()
row['phone'] = faker.phone.phoneNumber()
// row['timeZone'] = faker.address.timeZone();
})
doc.text('Split columns across pages if not fit in a single page', 14, 20)
doc.autoTable({
head: head,
body: body,
startY: 25,
// split overflowing columns into pages
horizontalPageBreak: true,
// repeat this column in split pages
// horizontalPageBreakRepeat: 'id',
})
return doc
}
// Split columns - shows how the overflowed columns split into pages with a given column repeated
examples.horizontalPageBreakRepeat = function () {
var doc = new jsPDF('l')
var head = headRows()
head[0].region = 'Region'
head[0].country = 'Country'
head[0].zipcode = 'Zipcode'
head[0].phone = 'Phone'
// head[0].timeZone = 'Timezone';
head[0]['text'] = 'Text'
var body = bodyRows(4)
body.forEach(function (row) {
row['text'] = faker.lorem.sentence(15)
row['zipcode'] = faker.address.zipCode()
row['country'] = faker.address.country()
row['region'] = faker.address.state()
row['phone'] = faker.phone.phoneNumber()
// row['timeZone'] = faker.address.timeZone();
})
doc.text(
'Split columns across pages if not fit in a single page with a column repeated.',
14,
20
)
doc.autoTable({
head: head,
body: body,
startY: 25,
// split overflowing columns into pages
horizontalPageBreak: true,
// repeat this column in split pages
horizontalPageBreakRepeat: 'id',
})
return doc
}
/*
|--------------------------------------------------------------------------
| Below is some helper functions for the examples
|--------------------------------------------------------------------------
*/
function headRows() {
return [
{ id: 'ID', name: 'Name', email: 'Email', city: 'City', expenses: 'Sum' },
]
}
function footRows() {
return [
{ id: 'ID', name: 'Name', email: 'Email', city: 'City', expenses: 'Sum' },
]
}
function columns() {
return [
{ header: 'ID', dataKey: 'id' },
{ header: 'Name', dataKey: 'name' },
{ header: 'Email', dataKey: 'email' },
{ header: 'City', dataKey: 'city' },
{ header: 'Exp', dataKey: 'expenses' },
]
}
function data(rowCount) {
rowCount = rowCount || 10
var body = []
for (var j = 1; j <= rowCount; j++) {
body.push({
id: j,
name: faker.name.findName(),
email: faker.internet.email(),
city: faker.address.city(),
expenses: faker.finance.amount(),
})
}
return body
}
function bodyRows(rowCount) {
rowCount = rowCount || 10
var body = []
for (var j = 1; j <= rowCount; j++) {
body.push({
id: j,
name: faker.name.findName(),
email: faker.internet.email(),
city: faker.address.city(),
expenses: faker.finance.amount(),
})
}
return body
}