forked from parallax/jsPDF
/
jspdf.plugin.htmltable.js
482 lines (446 loc) · 17.5 KB
/
jspdf.plugin.htmltable.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
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
/**
* jsPDF HTML Table PlugIn
* Copyright (c) 2014 Djordje Ungar <mail@djordjeungar.com>
*
*
* Licensed under the MIT License.
* http://opensource.org/licenses/mit-license
*/
(function(jsPDFAPI) {
'use strict';
var px2pt = 0.264583 * 72 / 25.4;
var padding = 3;
var margin = 20;
/**
* Removes trailing or leading space/tab characters
* @param str {String}
* @return {String}
*/
function trim(str) {
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
/**
* Converts The String To Title Case
* @param str {String}
* @return {String}
*/
function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
/**
* Loops through Object elements and runs the callback on each element
* @param o {Object}
* @param callback {function(value, index, o){}}
* @return [{}] An array of returns
*/
function each(o, callback) {
var returns = [];
for (var i in o) {
if (o.hasOwnProperty(i)){
returns.push(callback(o[i], i, o));
}
}
return returns;
}
function hex2rgb(hexcolor) {
var color = {};
if ((typeof hexcolor === 'string') && /^#[0-9A-Fa-f]{6}$/.test(hexcolor)) {
var hex = parseInt(hexcolor.substr(1), 16);
color.r = (hex >> 16) & 255;
color.g = (hex >> 8) & 255;
color.b = (hex & 255);
}
else {
color.r = color.g = color.b = 0;
}
return color;
}
/**
* Creates a 2D matrix and register each cell to a matrix cell,
* as if there was no colspan/rospan defined for any cell
*
* @param table {HTMLElement} (either thead, tbody and tfoot)
* @param options {Object} {
* @param width_scale_factor {Number} - scale factor to apply to the pixel width
* @param formatText {function(String){return String}} - function to be applied to each cell text
* }
* @returns {[][Object]} An Array of Arrays of cell Objects
*/
function createCellMatrix(table, options) {
if (table) {
options = options || {};
var m = {};
var scale_width = options.width_scale_factor || 1;
var column_widths = {};
var x, y, mx, my, xmax, ymax, $cell, cell, cspan, rspan, xo, txt;
for (y = 0, ymax = table.rows.length; y < ymax; y++) {
for (x = 0, xmax = table.rows[y].cells.length; x < xmax; x++) {
$cell = $(table.rows[y].cells[x]);
cspan = $cell.attr("colspan") | 0,
rspan = $cell.attr("rowspan") | 0;
cspan = cspan ? cspan : 1;
rspan = rspan ? rspan : 1;
if (options.formatText) {
txt = options.formatText($cell.text());
}
else {
txt = $cell.text();
}
cell = {
text: txt
,cspan: cspan
,rspan: rspan
,width: Math.floor($cell.outerWidth() * scale_width * px2pt)
,align: $cell.css('text-align') || 'center'
};
xo = x;
for ( ; m[y] && m[y][xo]; ++xo ); //skip already occupied cells in current row
/**
* There is a rounding error when we try to use cell widths
* multiplied by scaling factor and pixel to point conversion factor.
* That's why we collect all non-spaned column widths, and then
* later re-calculate the colspaned columns widths.
*/
if (cspan == 1 && !column_widths[xo]) {
column_widths[xo] = cell.width;
}
for ( mx = xo; mx < xo + cspan; ++mx ) { //mark matrix elements occupied by current cell with true
for ( my = y; my < y + rspan; ++my ) {
if ( !m[my] ) { //fill missing rows
m[my] = {};
}
if ( ! m[my][mx] ) {
m[my][mx] = cell;
if (cell.text) {
cell = {
text: false
,cspan: cell.cspan
,rspan: cell.rspan
,width: cell.width
,align: cell.align
};
}
}
}
}
}
}
// convert matrix object to an array of arrays, and fix column widths
var matrix = each(m, function(v, i, o) {
return each(v, function(sv, si, so) {
var x = parseInt(si,10);
if (sv.cspan > 1) {
sv.width = 0;
for (var i=0; i < sv.cspan; i++) {
sv.width += column_widths[x+i];
}
}
else {
sv.width = column_widths[x];
}
return sv;
});
});
return matrix;
}
return false;
}
/**
* Adds a new page, resets the cursor position, and optionally prints table headers
* @returns {jsPDF}
*/
jsPDFAPI.newPage = function() {
this.addPage();
this.moveTo(this.margins.left, this.margins.top);
if (this.thead.matrix) {
this.renderTableMatrix(this.thead.matrix, this.thead || this.tbody);
}
return this;
};
/**
* Sets the internal cursor position.
* @param x {Number} The horizontal position
* @param y {Number} The vertical position
* @returns {jsPDF}
*/
jsPDFAPI.moveTo = function(x, y) {
if (!this.cursor) {
this.cursor = {};
}
this.cursor.x = x;
this.cursor.y = y;
return this;
};
/**
* Store the font and drawing styles
* @param name {String} The name of the style setting
* @param value {Mixed} The value of the style setting
* @returns {jsPDF}
*/
var style = {};
jsPDFAPI.setStyle = function(name, value) {
if (value === undefined) {
for (var i in name) {
if (name.hasOwnProperty(i)) {
this.setStyle(i, name[i]);
}
}
return this;
}
switch (name) {
case 'fontName':
style[name] = value;
return this.setFont(value);
case 'fontSize':
style[name] = value;
return this.setFontSize(value);
case 'fontStyle':
style[name] = value;
return this.setFontStyle(value);
case 'fontColor':
style[name] = value;
return this.setTextColor(value);
case 'fillColor':
style[name] = value;
if (value) {
value = hex2rgb(value);
return this.setFillColor(value.r, value.g, value.b);
}
break;
case 'lineColor':
style[name] = value;
if (value) {
value = hex2rgb(value);
return this.setDrawColor(value.r, value.g, value.b);
}
break;
case 'align':
style[name] = value;
break;
}
return this;
};
/**
* Get the font and drawing styles
* @param name {String} The name of the style setting
* @returns {Object} The value of the style setting
*/
jsPDFAPI.getStyle = function(name) {
if (name === undefined) {
return $.extend({}, style);
}
if (style.hasOwnProperty(name)) {
return style[name];
}
return undefined;
};
/**
* Pushes the current font and drawing styles onto the stack.
* @returns {jsPDF}
*/
var contexts;
jsPDFAPI.saveStyles = function() {
if ( ! contexts ) {
contexts = [];
}
contexts.push($.extend({}, style));
return this;
};
/**
* Pops the font and drawing styles from the stack, and sets the styles.
* @returns {jsPDF}
*/
jsPDFAPI.restoreStyles = function() {
if (contexts && contexts.length ) {
this.setStyle(contexts.pop());
}
return this;
};
/**
* Renders the table portion.
* @param matrix {[][Object]} An array of arrays of cells, usually the return of {createCellMatrix()}
* @returns {jsPDF}
*/
jsPDFAPI.renderTableMatrix = function(matrix, options) {
var x, y, cell, line_height, cell_indexes, cell_widths, cell_texts, txt, i, cursor = this.cursor;
options = $.extend({}, this.tbody, options || {});
this.saveStyles();
this.setStyle(options);
var style = '';
if (options.fillColor && options.lineColor) {
style = 'FD';
}
else if (options.fillColor) {
style = 'F';
}
else if (options.lineColor) {
style = 'S';
}
var getIndexes = function(val, i) { return i;};
var getWidths = function(val, i) { return val.width;};
var getTexts = function(val, i) { return val.text || '';};
for (y = 0; y < matrix.length; y++) {
cell_indexes = each(matrix[y], getIndexes);
cell_widths = each(matrix[y], getWidths);
cell_texts = each(matrix[y], getTexts);
line_height = this.calculateLineHeight(cell_indexes, cell_widths, cell_texts);
for (x = 0; x < matrix[y].length; x++) {
cell = matrix[y][x];
if (cell.text !== false) {
if (style !== '') {
if (false || options.fillColor) {
// I don't know why but it needs to be reset
this.setStyle('fillColor', options.fillColor);
}
this.rect(cursor.x, cursor.y, cell.width, line_height * cell.rspan, style);
}
txt = cell_texts[x];
switch (options.align || cell.align) {
case 'right':
if (!(txt instanceof Array)) {
txt = [txt];
}
for (i = 0; i < txt.length; i++) {
var textSize = this.getStringUnitWidth(txt[i]) * this.internal.getFontSize();
this.text(txt[i], cursor.x + cell_widths[x] - textSize - padding, cursor.y + this.internal.getLineHeight()*(i+1));
}
break;
case 'center':
if (!(txt instanceof Array)) {
txt = [txt];
}
for (i = 0; i < txt.length; i++) {
var textSize = this.getStringUnitWidth(txt[i]) * this.internal.getFontSize();
this.text(txt[i], cursor.x + (cell_widths[x] - textSize)/2, cursor.y + this.internal.getLineHeight()*(i+1));
}
break;
default:
// 'left'
this.text(txt, cursor.x + padding, cursor.y + this.internal.getLineHeight());
}
cursor.x += cell_widths[x];
}
else if (cell.rspan > 1) {
cursor.x += cell_widths[x];
}
}
cursor.y += line_height;
cursor.x = this.margins.left;
if ((cursor.y + line_height + margin) >= this.internal.pageSize.height - this.margins.bottom) {
this.newPage();
}
}
this.cursor = cursor;
this.restoreStyles();
return this;
};
/**
* Parse and the render the html table object to jsPDF
* @example
*
* var options = {
* fit_to_page: true // will scale the table to fit the page width
* ,tbody: {
* fontName: 'helvetica'
* ,fontSize: 8
* ,fontColor: '#000000'
* }
* ,thead: {
* repeatOnEveryPage: true
* ,toTitleCase: true
* ,fontStyle: 'bold'
* ,fillColor: '#DADADA' // http://youtu.be/lNYcviXK4rg
* ,fontColor: '#000000'
* }
* ,margins: {
* bottom: 12
* }
* }
* doc.htmlTable(x, y, document.getElementById('#yourtable'), options);
* doc.save(filename);
*
* @param x {Number} The horizontal position
* @param y {Number} The vertical position
* @param table {HTMLElement} The table element to parse
* @param {Object[]} config
* @returns {jsPDF}
*/
jsPDFAPI.htmlTable = function(x, y, table, options) {
'use strict';
if (!table) {
throw 'No data for PDF table';
}
options = options || {};
this.margins = {
left: x
,top: margin
,bottom: margin
,right: margin
};
if (options['margins']) {
this.margins.left = (options.margins.left === undefined) ? this.margins.left : options.margins.left;
this.margins.top = (options.margins.top === undefined) ? this.margins.top : options.margins.top;
this.margins.bottom = (options.margins.bottom === undefined) ? this.margins.bottom : options.margins.bottom;
this.margins.right = (options.margins.right === undefined) ? this.margins.right : options.margins.right;
}
this.margins.width = this.internal.pageSize.width - this.margins.left - this.margins.right;
var tbody_options = options['tbody'] || {};
this.tbody = {
fontName: tbody_options['fontName'] || 'helvetica'
,fontSize: tbody_options['fontSize'] || 10
,fontStyle: tbody_options['fontStyle'] || 'normal'
,fontColor: tbody_options['fontColor'] || '#000000'
,fillColor: tbody_options['fillColor'] || false
,lineColor: tbody_options['lineColor'] || '#000000'
,align: tbody_options['align'] || false
};
var thead_options = options['thead'] || {};
this.thead = {
fontName: thead_options['fontName'] || this.tbody.fontName
,fontSize: thead_options['fontSize'] || this.tbody.fontSize
,fontStyle: thead_options['fontStyle'] || this.tbody.fontStyle
,fontColor: thead_options['fontColor'] || this.tbody.fontColor
,fillColor: thead_options['fillColor'] || this.tbody.fillColor
,lineColor: thead_options['lineColor'] || this.tbody.lineColor
,repeatOnEveryPage: thead_options['repeatOnEveryPage'] || false
,align: thead_options['align'] || false
};
var tfoot_options = options['tfoot'] || {};
this.tfoot = {
fontName: tfoot_options['fontName'] || this.tbody.fontName
,fontSize: tfoot_options['fontSize'] || this.tbody.fontSize
,fontStyle: tfoot_options['fontStyle'] || this.tbody.fontStyle
,fontColor: tfoot_options['fontColor'] || this.tbody.fontColor
,fillColor: tfoot_options['fillColor'] || this.tbody.fillColor
,lineColor: thead_options['lineColor'] || this.tbody.lineColor
,align: tfoot_options['align'] || false
};
var $table = $(table);
var matrix_options = {};
if (options.fit_to_page) {
// We will asume that you want the table to be stretched to the longer side (297mm) of A4
// A4 = 8.27x11.69" x72points/inch = 595x842 points
// 842 / 75 * 100 = 1122.66.. px
var available_width = 1122 - (this.margins.left+this.margins.right)/px2pt;
matrix_options.width_scale_factor = available_width/$table.width();
}
matrix_options.formatText = trim;
var body = createCellMatrix($table.find('tbody').get(0), matrix_options);
var foot = createCellMatrix($table.find('tfoot').get(0), matrix_options);
if (options['thead'] && options['thead']['toTitleCase']) {
matrix_options.formatText = toTitleCase;
}
var head = createCellMatrix($table.find('thead').get(0), matrix_options);
if (this.thead && this.thead.repeatOnEveryPage) {
this.thead.matrix = head;
}
this.cursor = {};
this.moveTo(x, y);
this.setStyle(this.tbody);
this.renderTableMatrix(head, this.thead);
this.renderTableMatrix(body, this.tbody);
this.renderTableMatrix(foot, this.tfoot);
$table = null;
return this;
};
})(jsPDF.API);