forked from simonbengtsson/jsPDF-AutoTable
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexamples.js
426 lines (374 loc) · 13.8 KB
/
examples.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
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
/*
|--------------------------------------------------------------------------
| 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
| are added to the examples below. For example they return their jspdf
| doc instance and gets generated data from the library faker.js. However you
| can of course use this plugin how you wish and the simplest first example
| below would look like this without any extras:
|
| var columns = ["ID", "Name", "Age", "City"];
|
| var data = [
| [1, "Jonathan", 25, "Gothenburg"],
| [2, "Simon", 23, "Gothenburg"],
| [3, "Hanna", 21, "Stockholm"]
| ];
|
| var doc = new jsPDF('p', 'pt');
| doc.autoTable(columns, data);
| doc.save("table.pdf");
|
*/
var faker = window.faker;
var base64Img = null;
var examples = {};
// Default - shows what a default table looks like
examples.auto = function () {
var doc = new jsPDF();
doc.autoTable(getColumns(), getData());
return doc;
};
// Minimal - shows how compact tables can be drawn
examples.minimal = function () {
var doc = new jsPDF();
doc.autoTable(getColumns(), getData(), {
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 columnsLong = getColumns().concat([
{title: "Title with\nlinebreak", dataKey: "text2"},
{title: "Long text column", dataKey: "text"},
]);
doc.text(7, 15, "Overflow 'ellipsize' (default)");
doc.autoTable(columnsLong, getData(), {
startY: 20,
margin: {horizontal: 7},
styles: {columnWidth: 'wrap'},
columnStyles: {text: {columnWidth: 'auto'}}
});
doc.text("Overflow 'hidden'", 7, doc.autoTable.previous.finalY + 10);
doc.autoTable(columnsLong, getData(), {
startY: doc.autoTable.previous.finalY + 15,
margin: {horizontal: 7},
styles: {overflow: 'hidden', columnWidth: 'wrap'},
columnStyles: {text: {columnWidth: 'auto'}}
});
doc.text("Overflow 'linebreak'", 7, doc.autoTable.previous.finalY + 10);
doc.autoTable(columnsLong, getData(3), {
startY: doc.autoTable.previous.finalY + 15,
margin: {horizontal: 7},
bodyStyles: {valign: 'top'},
styles: {overflow: 'linebreak', columnWidth: 'wrap'},
columnStyles: {text: {columnWidth: 'auto'}}
});
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('A story about a person', 14, 22);
doc.setFontSize(11);
doc.setTextColor(100);
var text = doc.splitTextToSize(shuffleSentence(faker.lorem.words(55)) + '.', doc.internal.pageSize.width - 35, {});
doc.text(text, 14, 30);
var cols = getColumns();
cols.splice(0, 2);
doc.autoTable(cols, getData(40), {startY: 50, showHeader: 'firstPage'});
doc.text(text, 14, doc.autoTable.previous.finalY + 10);
return doc;
};
// Multiple - shows how multiple tables can be drawn both horizontally and vertically
examples.multiple = function () {
var doc = new jsPDF();
doc.setFontSize(22);
doc.text("Multiple tables", 14, 20);
doc.setFontSize(12);
doc.autoTable(getColumns(), getData(10), {startY: 30});
let first = doc.autoTable.previous;
doc.autoTable(getColumns().slice(0, 2), getData(30), {
startY: first.finalY + 10,
showHeader: 'firstPage',
margin: {right: 107}
});
// Reset page to the same as before previous table
doc.setPage(1 + doc.internal.getCurrentPageInfo().pageNumber - doc.autoTable.previous.pageCount);
doc.autoTable(getColumns().slice(0, 2), getData(30), {
startY: first.finalY + 10,
showHeader: 'firstPage',
margin: {left: 107}
});
for (var j = 0; j < 6; j++) {
doc.autoTable(getColumns(), getData(9), {
startY: doc.autoTable.previous.finalY + 10,
pageBreak: 'avoid',
});
}
return doc;
};
// From html - shows how pdf tables can be be drawn from html tables
examples.html = function () {
var doc = new jsPDF();
doc.text("From HTML", 14, 16);
var elem = document.getElementById("basic-table");
var res = doc.autoTableHtmlToJson(elem);
doc.autoTable(res.columns, res.data, {startY: 20});
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}";
var pageContent = function (data) {
// HEADER
doc.setFontSize(20);
doc.setTextColor(40);
doc.setFontStyle('normal');
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 " + data.pageCount;
// Total page number plugin only available in jspdf v1.0+
if (typeof doc.putTotalPages === 'function') {
str = str + " of " + totalPagesExp;
}
doc.setFontSize(10);
doc.text(str, data.settings.margin.left, doc.internal.pageSize.height - 10);
};
doc.autoTable(getColumns(), getData(40), {
addPageContent: pageContent,
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
jsPDF.autoTableSetDefaults({
columnStyles: {id: {fontStyle: 'bold'}},
headerStyles: {fillColor: 0},
});
var doc = new jsPDF();
// Document defaults
doc.autoTableSetDefaults({
headerStyles: {fillColor: [155, 89, 182]}, // Purple
margin: {top: 25},
addPageContent: function(data) {
doc.setFontSize(20);
doc.text('Document specific header', data.settings.margin.left, 20);
}
});
doc.autoTable(getColumns(), getData());
doc.addPage();
doc.autoTable(getColumns(), getData(), {
// Will override document and global headerStyles
headerStyles: {fillColor: [231, 76, 60]} // Red
});
// Reset defaults
doc.autoTableSetDefaults(null);
jsPDF.autoTableSetDefaults(null);
return doc;
};
// Horizontal - shows how tables can be drawn with horizontal headers
examples.horizontal = function () {
var doc = new jsPDF();
doc.autoTable(getColumns().splice(1, 4), getData(), {
showHeader: 'never',
columnStyles: {
name: {fillColor: [41, 128, 185], textColor: 255, fontStyle: 'bold'}
}
});
return doc;
};
// Custom style - shows how custom styles can be applied to tables
examples.spans = function () {
var doc = new jsPDF('p', 'pt');
doc.setFontSize(12);
doc.setTextColor(0);
doc.setFontStyle('bold');
doc.text('Col and row span', 40, 50);
var data = getData(80);
data.sort(function (a, b) {
return parseFloat(b.expenses) - parseFloat(a.expenses);
});
doc.autoTable(getColumns(), data, {
theme: 'grid',
startY: 60,
drawRow: function (row, data) {
// Colspan
doc.setFontStyle('bold');
doc.setFontSize(10);
if (row.index === 0) {
doc.setTextColor(200, 0, 0);
doc.rect(data.settings.margin.left, row.y, data.table.width, 20, 'S');
doc.autoTableText("Priority Group", data.settings.margin.left + data.table.width / 2, row.y + row.height / 2, {
halign: 'center',
valign: 'middle'
});
data.cursor.y += 20;
} else if (row.index === 5) {
doc.rect(data.settings.margin.left, row.y, data.table.width, 20, 'S');
doc.autoTableText("Other Groups", data.settings.margin.left + data.table.width / 2, row.y + row.height / 2, {
halign: 'center',
valign: 'middle'
});
data.cursor.y += 20;
}
if (row.index % 5 === 0) {
var posY = row.y + row.height * 6 + data.settings.margin.bottom;
if (posY > doc.internal.pageSize.height) {
data.addPage();
}
}
},
drawCell: function (cell, data) {
// Rowspan
if (data.column.dataKey === 'id') {
if (data.row.index % 5 === 0) {
doc.rect(cell.x, cell.y, data.table.width, cell.height * 5, 'S');
doc.autoTableText(data.row.index / 5 + 1 + '', cell.x + cell.width / 2, cell.y + cell.height * 5 / 2, {
halign: 'center',
valign: 'middle'
});
}
return false;
}
}
});
return doc;
};
// Themes - shows how the different themes looks
examples.themes = function () {
var doc = new jsPDF();
doc.setFontSize(12);
doc.setFontStyle('bold');
doc.text('Theme "striped"', 14, 16);
doc.autoTable(getColumns(), getData(), {startY: 20});
doc.text('Theme "grid"', 14, doc.autoTable.previous.finalY + 10);
doc.autoTable(getColumns(), getData(), {startY: doc.autoTable.previous.finalY + 14, theme: 'grid'});
doc.text('Theme "plain"', 14, doc.autoTable.previous.finalY + 10);
doc.autoTable(getColumns(), getData(), {startY: doc.autoTable.previous.finalY + 14, theme: 'plain'});
return doc;
};
// Custom style - shows how custom styles can be applied to tables
examples.custom = function () {
var doc = new jsPDF();
doc.autoTable(getColumns().slice(1, 5), getData(20), {
tableLineColor: [189, 195, 199],
tableLineWidth: 0.75,
styles: {
font: 'courier',
lineColor: [44, 62, 80],
lineWidth: 0.75
},
headerStyles: {
fillColor: [44, 62, 80],
fontSize: 15
},
bodyStyles: {
fillColor: [52, 73, 94],
textColor: 240
},
alternateRowStyles: {
fillColor: [74, 96, 117]
},
columnStyles: {
email: {
fontStyle: 'bold'
}
},
/*parsedInput: function (cell, data) {
if (data.column.dataKey === 'expenses') {
cell.styles.halign = 'right';
if (cell.raw > 600) {
cell.styles.textColor = [255, 100, 100];
cell.styles.fontStyle = 'bolditalic';
}
cell.text = '$' + cell.text;
} else if (data.column.dataKey === 'name') {
cell.text = cell.raw.split(' ')[0]; // only first name
}
}*/
});
return doc;
};
/*
|--------------------------------------------------------------------------
| Below is some helper functions for the examples
|--------------------------------------------------------------------------
*/
// Returns a new array each time to avoid pointer issues
var getColumns = function () {
return [
{title: "ID", dataKey: "id"},
{title: "Name", dataKey: "name"},
{title: "Email", dataKey: "email"},
{title: "City", dataKey: "city"},
{title: "Expenses", dataKey: "expenses"}
];
};
// Uses the faker.js library to get random data.
function getData(rowCount) {
rowCount = rowCount || 4;
//var sentence = "Minima quis totam nobis nihil et molestiae architecto accusantium qui necessitatibus sit ducimus cupiditate qui ullam et aspernatur esse et dolores ut voluptatem odit quasi ea sit ad sint voluptatem est dignissimos voluptatem vel adipisci facere consequuntur et reprehenderit cum unde debitis ab cumque sint quo ut officiis rerum aut quia quia expedita ut consectetur animiqui voluptas suscipit Monsequatur";
var sentence = faker.lorem.words(20);
var data = [];
for (var j = 1; j <= rowCount; j++) {
data.push({
id: j,
name: faker.name.findName(),
email: faker.internet.email(),
country: faker.address.country(),
city: faker.address.city(),
expenses: faker.finance.amount(),
text: shuffleSentence(sentence),
text2: faker.lorem.words(1)
});
}
return data;
}
function shuffleSentence(words) {
if (typeof words === 'string') return words;
words = words || faker.lorem.words(8);
var str = faker.helpers.shuffle(words).join(' ').trim();
return str.charAt(0).toUpperCase() + str.slice(1);
}
imgToBase64('document.jpg', function(base64) {
base64Img = base64;
});
// You could either use a function similar to this or pre convert an image with for example http://dopiaza.org/tools/datauri
// http://stackoverflow.com/questions/6150289/how-to-convert-image-into-base64-string-using-javascript
function imgToBase64(url, callback) {
if (!window.FileReader) {
callback(null);
return;
}
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function() {
var reader = new FileReader();
reader.onloadend = function() {
callback(reader.result.replace('text/xml', 'image/jpeg'));
};
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', url);
xhr.send();
}