forked from parallax/jsPDF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jspdf.plugin.cell.js
398 lines (336 loc) · 13.3 KB
/
jspdf.plugin.cell.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
/** ====================================================================
* jsPDF Cell plugin
* Copyright (c) 2013 Youssef Beddad, youssef.beddad@gmail.com
* 2013 Eduardo Menezes de Morais, eduardo.morais@usp.br
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* ====================================================================
*/
(function (jsPDFAPI) {
'use strict';
/*jslint browser:true */
/*global document: false, jsPDF */
var fontName,
fontSize,
fontStyle,
padding = 3,
margin = 13,
headerFunction,
lastCellPos = { x: undefined, y: undefined, w: undefined, h: undefined, ln: undefined },
pages = 1,
setLastCellPosition = function (x, y, w, h, ln) {
lastCellPos = { 'x': x, 'y': y, 'w': w, 'h': h, 'ln': ln };
},
getLastCellPosition = function () {
return lastCellPos;
};
jsPDFAPI.setHeaderFunction = function (func) {
headerFunction = func;
};
jsPDFAPI.setMargin = function (marg) {
margin = marg;
}
jsPDFAPI.getTextDimensions = function (txt) {
fontName = this.internal.getFont().fontName;
fontSize = this.internal.getFontSize();
fontStyle = this.internal.getFont().fontStyle;
// 1 pixel = 0.264583 mm and 1 mm = 72/25.4 point
var px2pt = 0.264583 * 72 / 25.4,
dimensions,
text;
text = document.createElement('font');
text.id = "jsPDFCell";
text.style.fontStyle = fontStyle;
text.style.fontName = fontName;
text.style.fontSize = fontSize + 'pt';
text.innerText = txt;
document.body.appendChild(text);
dimensions = { w: (text.offsetWidth + 1) * px2pt, h: (text.offsetHeight + 1) * px2pt};
document.body.removeChild(text);
return dimensions;
};
jsPDFAPI.cellAddPage = function () {
this.addPage();
setLastCellPosition(undefined, undefined, undefined, undefined, undefined);
pages += 1;
};
jsPDFAPI.cellInitialize = function () {
lastCellPos = { x: undefined, y: undefined, w: undefined, h: undefined, ln: undefined };
pages = 1;
};
jsPDFAPI.cell = function (x, y, w, h, txt, ln, align) {
var curCell = getLastCellPosition();
// If this is not the first cell, we must change its position
if (curCell.ln !== undefined) {
if (curCell.ln === ln) {
//Same line
x = curCell.x + curCell.w;
y = curCell.y;
} else {
//New line
if ((curCell.y + curCell.h + h + margin) >= this.internal.pageSize.height) {
this.cellAddPage();
if (this.printHeaders && this.tableHeaderRow) {
this.printHeaderRow(ln);
}
}
//We ignore the passed y: the lines may have diferent heights
y = (getLastCellPosition().y + getLastCellPosition().h);
}
}
if (txt[0] !== '') {
if (this.printingHeaderRow || txt.fill) {
this.setFillColor(200,200,200);
this.rect(x, y, w, h, 'FD');
} else {
this.rect(x, y, w, h);
}
if (align === 'right') {
if (txt instanceof Array) {
for(var i = 0; i<txt.length; i++) {
var currentLine = txt[i];
var textSize = this.getStringUnitWidth(currentLine) * this.internal.getFontSize();
this.text(currentLine, x + w - textSize - padding, y + this.internal.getLineHeight()*(i+1));
}
}
} else {
this.text(txt, x + padding, y + this.internal.getLineHeight());
}
}
setLastCellPosition(x, y, w, h, ln);
return this;
};
/**
* Return an array containing all of the owned keys of an Object
* @type {Function}
* @return {String[]} of Object keys
*/
jsPDFAPI.getKeys = (typeof Object.keys === 'function')
? function (object) {
if (!object) {
return [];
}
return Object.keys(object);
}
: function (object) {
var keys = [],
property;
for (property in object) {
if (object.hasOwnProperty(property)) {
keys.push(property);
}
}
return keys;
};
/**
* Return the maximum value from an array
* @param array
* @param comparisonFn
* @returns {*}
*/
jsPDFAPI.arrayMax = function (array, comparisonFn) {
var max = array[0],
i,
ln,
item;
for (i = 0, ln = array.length; i < ln; i += 1) {
item = array[i];
if (comparisonFn) {
if (comparisonFn(max, item) === -1) {
max = item;
}
} else {
if (item > max) {
max = item;
}
}
}
return max;
};
/**
* Create a table from a set of data.
* @param {Object[]} data As array of objects containing key-value pairs
* @param {String[]} [headers] Omit or null to auto-generate headers at a performance cost
* @param {Object} [config.printHeaders] True to print column headers at the top of every page
* @param {Object} [config.autoSize] True to dynamically set the column widths to match the widest cell value
* @param {Object} [config.autoStretch] True to force the table to fit the width of the page
*/
jsPDFAPI.table = function (data, headers, config) {
var headerNames = [],
headerPrompts = [],
header,
autoSize,
printHeaders,
autoStretch,
i,
ln,
columnMatrix = {},
columnWidths = {},
columnData,
column,
columnMinWidths = [],
j,
tableHeaderConfigs = [],
model,
jln,
func;
/**
* @property {Number} lnMod
* Keep track of the current line number modifier used when creating cells
*/
this.lnMod = 0;
if (config) {
autoSize = config.autoSize || false;
printHeaders = this.printHeaders = config.printHeaders || true;
autoStretch = config.autoStretch || true;
}
if (!data) {
throw 'No data for PDF table';
}
// Set headers
if (headers === undefined || (headers === null)) {
// No headers defined so we derive from data
headerNames = this.getKeys(data[0]);
} else if (headers[0] && (typeof headers[0] !== 'string')) {
// Split header configs into names and prompts
for (i = 0, ln = headers.length; i < ln; i += 1) {
header = headers[i];
headerNames.push(header.name);
headerPrompts.push(header.prompt);
columnWidths[header.name] = header.width;
}
} else {
headerNames = headers;
}
if (config.autoSize) {
// Create Columns Matrix
func = function (rec) {
return rec[header];
};
for (i = 0, ln = headerNames.length; i < ln; i += 1) {
header = headerNames[i];
columnMatrix[header] = data.map(
func
);
// get header width
columnMinWidths.push(this.getTextDimensions(headerPrompts[i] || header).w);
column = columnMatrix[header];
// get cell widths
for (j = 0, ln = column.length; j < ln; j += 1) {
columnData = column[j];
columnMinWidths.push(this.getTextDimensions(columnData).w);
}
// get final column width
columnWidths[header] = jsPDFAPI.arrayMax(columnMinWidths);
}
}
// -- Construct the table
if (config.printHeaders) {
var lineHeight = this.calculateLineHeight(headerNames, columnWidths, headerPrompts.length?headerPrompts:headerNames);
// Construct the header row
for (i = 0, ln = headerNames.length; i < ln; i += 1) {
header = headerNames[i];
tableHeaderConfigs.push([margin, margin, columnWidths[header], lineHeight, String(headerPrompts.length ? headerPrompts[i] : header)]);
}
// Store the table header config
this.setTableHeaderRow(tableHeaderConfigs);
// Print the header for the start of the table
this.printHeaderRow(1);
}
// Construct the data rows
for (i = 0, ln = data.length; i < ln; i += 1) {
var lineHeight;
model = data[i];
lineHeight = this.calculateLineHeight(headerNames, columnWidths, model);
for (j = 0, jln = headerNames.length; j < jln; j += 1) {
var width;
header = headerNames[j];
width = columnWidths[header];
if (model[header].colspan > 1) {
for (var k = 1; k < model[header].colspan; k++) {
width += columnWidths[ headerNames[j+k] ];
}
j += model[header].colspan - 1;
}
this.cell(margin, margin, width, lineHeight, model[header], i + 2, model[header].align);
}
}
return this;
};
/**
* Calculate the height for containing the highest column
* @param {String[]} headerNames is the header, used as keys to the data
* @param {Integer[]} columnWidths is size of each column
* @param {Object[]} model is the line of data we want to calculate the height of
*/
jsPDFAPI.calculateLineHeight = function (headerNames, columnWidths, model) {
var header, lineHeight = 0, splitText;
for (var j = 0; j < headerNames.length; j++) {
header = headerNames[j];
splitText = this.splitTextToSize(String(model[header]), columnWidths[header] - padding);
if (model[header] !== undefined) {
splitText.colspan = model[header].colspan;
splitText.rowspan = model[header].rowspan;
splitText.align = model[header].align;
splitText.fill = model[header].fill;
}
model[header] = splitText;
var h = this.internal.getLineHeight() * model[header].length + padding;
if (h > lineHeight)
lineHeight = h;
}
return lineHeight;
};
/**
* Store the config for outputting a table header
* @param {Object[]} config
* An array of cell configs that would define a header row: Each config matches the config used by jsPDFAPI.cell
* except the ln parameter is excluded
*/
jsPDFAPI.setTableHeaderRow = function (config) {
this.tableHeaderRow = config;
};
/**
* Output the store header row
* @param lineNumber The line number to output the header at
*/
jsPDFAPI.printHeaderRow = function (lineNumber) {
if (!this.tableHeaderRow) {
throw 'Property tableHeaderRow does not exist.';
}
var tableHeaderCell,
tmpArray,
i,
ln;
this.printingHeaderRow = true;
if (headerFunction !== undefined) {
var position = headerFunction(this, pages);
setLastCellPosition(position[0], position[1], position[2], position[3], -1);
}
this.setFontStyle('bold');
for (i = 0, ln = this.tableHeaderRow.length; i < ln; i += 1) {
tableHeaderCell = this.tableHeaderRow[i];
tmpArray = [].concat(tableHeaderCell);
this.cell.apply(this, tmpArray.concat(lineNumber));
}
this.setFontStyle('normal');
this.printingHeaderRow = false;
};
})(jsPDF.API);