| 9 |
lars |
1 |
/** ====================================================================
|
|
|
2 |
* jsPDF Cell plugin
|
|
|
3 |
* Copyright (c) 2013 Youssef Beddad, youssef.beddad@gmail.com
|
|
|
4 |
*
|
|
|
5 |
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
6 |
* a copy of this software and associated documentation files (the
|
|
|
7 |
* "Software"), to deal in the Software without restriction, including
|
|
|
8 |
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
9 |
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
10 |
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
11 |
* the following conditions:
|
|
|
12 |
*
|
|
|
13 |
* The above copyright notice and this permission notice shall be
|
|
|
14 |
* included in all copies or substantial portions of the Software.
|
|
|
15 |
*
|
|
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
18 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
20 |
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
21 |
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
22 |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
23 |
* ====================================================================
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
(function (jsPDFAPI) {
|
|
|
27 |
'use strict';
|
|
|
28 |
/*jslint browser:true */
|
|
|
29 |
/*global document: false, jsPDF */
|
|
|
30 |
|
|
|
31 |
var maxLn = 0,
|
|
|
32 |
lnP = 0,
|
|
|
33 |
fontName,
|
|
|
34 |
fontSize,
|
|
|
35 |
fontStyle,
|
|
|
36 |
lastCellPos = { x: undefined, y: undefined, w: undefined, h: undefined, ln: undefined },
|
|
|
37 |
pages = 1,
|
|
|
38 |
newPage = false,
|
|
|
39 |
setLastCellPosition = function (x, y, w, h, ln) {
|
|
|
40 |
lastCellPos = { x: x, y: y, w: w, h: h, ln: ln };
|
|
|
41 |
},
|
|
|
42 |
getLastCellPosition = function () {
|
|
|
43 |
return lastCellPos;
|
|
|
44 |
},
|
|
|
45 |
setMaxLn = function (x) {
|
|
|
46 |
maxLn = x;
|
|
|
47 |
},
|
|
|
48 |
getMaxLn = function () {
|
|
|
49 |
return maxLn;
|
|
|
50 |
},
|
|
|
51 |
setLnP = function (x) {
|
|
|
52 |
lnP = x;
|
|
|
53 |
},
|
|
|
54 |
getLnP = function (x) {
|
|
|
55 |
return lnP;
|
|
|
56 |
};
|
|
|
57 |
|
|
|
58 |
jsPDFAPI.getTextDimensions = function (txt) {
|
|
|
59 |
fontName = this.internal.getFont().fontName;
|
|
|
60 |
fontSize = this.internal.getFontSize();
|
|
|
61 |
fontStyle = this.internal.getFont().fontStyle;
|
|
|
62 |
|
|
|
63 |
// 1 pixel = 0.264583 mm and 1 mm = 72/25.4 point
|
|
|
64 |
var px2pt = 0.264583 * 72 / 25.4,
|
|
|
65 |
dimensions,
|
|
|
66 |
text;
|
|
|
67 |
|
|
|
68 |
text = document.createElement('font');
|
|
|
69 |
text.id = "jsPDFCell";
|
|
|
70 |
text.style.fontStyle = fontStyle;
|
|
|
71 |
text.style.fontName = fontName;
|
|
|
72 |
text.style.fontSize = fontSize + 'pt';
|
|
|
73 |
text.innerText = txt;
|
|
|
74 |
|
|
|
75 |
document.body.appendChild(text);
|
|
|
76 |
|
|
|
77 |
dimensions = { w: (text.offsetWidth + 1) * px2pt, h: (text.offsetHeight + 1) * px2pt};
|
|
|
78 |
|
|
|
79 |
document.body.removeChild(text);
|
|
|
80 |
|
|
|
81 |
return dimensions;
|
|
|
82 |
};
|
|
|
83 |
|
|
|
84 |
jsPDFAPI.cellAddPage = function () {
|
|
|
85 |
this.addPage();
|
|
|
86 |
setLastCellPosition(undefined, undefined, undefined, undefined, undefined);
|
|
|
87 |
newPage = true;
|
|
|
88 |
pages += 1;
|
|
|
89 |
setLnP(1);
|
|
|
90 |
};
|
|
|
91 |
|
|
|
92 |
jsPDFAPI.cellInitialize = function () {
|
|
|
93 |
maxLn = 0;
|
|
|
94 |
lastCellPos = { x: undefined, y: undefined, w: undefined, h: undefined, ln: undefined };
|
|
|
95 |
pages = 1;
|
|
|
96 |
newPage = false;
|
|
|
97 |
setLnP(0);
|
|
|
98 |
};
|
|
|
99 |
|
|
|
100 |
jsPDFAPI.cell = function (x, y, w, h, txt, ln) {
|
|
|
101 |
this.lnMod = this.lnMod === undefined ? 0 : this.lnMod;
|
|
|
102 |
if (this.printingHeaderRow !== true && this.lnMod !== 0) {
|
|
|
103 |
ln = ln + this.lnMod;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
if ((((ln * h) + y + (h * 2)) / pages) >= this.internal.pageSize.height && pages === 1 && !newPage) {
|
|
|
107 |
this.cellAddPage();
|
|
|
108 |
|
|
|
109 |
if (this.printHeaders && this.tableHeaderRow) {
|
|
|
110 |
this.printHeaderRow(ln);
|
|
|
111 |
this.lnMod += 1;
|
|
|
112 |
ln += 1;
|
|
|
113 |
}
|
|
|
114 |
if (getMaxLn() === 0) {
|
|
|
115 |
setMaxLn(Math.round((this.internal.pageSize.height - (h * 2)) / h));
|
|
|
116 |
}
|
|
|
117 |
} else if (newPage && getLastCellPosition().ln !== ln && getLnP() === getMaxLn()) {
|
|
|
118 |
this.cellAddPage();
|
|
|
119 |
|
|
|
120 |
if (this.printHeaders && this.tableHeaderRow) {
|
|
|
121 |
this.printHeaderRow(ln);
|
|
|
122 |
this.lnMod += 1;
|
|
|
123 |
ln += 1;
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
var curCell = getLastCellPosition(),
|
|
|
128 |
dim = this.getTextDimensions(txt),
|
|
|
129 |
isNewLn = 1;
|
|
|
130 |
if (curCell.x !== undefined && curCell.ln === ln) {
|
|
|
131 |
x = curCell.x + curCell.w;
|
|
|
132 |
}
|
|
|
133 |
if (curCell.y !== undefined && curCell.y === y) {
|
|
|
134 |
y = curCell.y;
|
|
|
135 |
}
|
|
|
136 |
if (curCell.h !== undefined && curCell.h === h) {
|
|
|
137 |
h = curCell.h;
|
|
|
138 |
}
|
|
|
139 |
if (curCell.ln !== undefined && curCell.ln === ln) {
|
|
|
140 |
ln = curCell.ln;
|
|
|
141 |
isNewLn = 0;
|
|
|
142 |
}
|
|
|
143 |
if (newPage) {
|
|
|
144 |
y = h * (getLnP() + isNewLn);
|
|
|
145 |
} else {
|
|
|
146 |
y = (y + (h * Math.abs(getMaxLn() * pages - ln - getMaxLn())));
|
|
|
147 |
}
|
|
|
148 |
this.rect(x, y, w, h);
|
|
|
149 |
this.text(txt, x + 3, y + h - 3);
|
|
|
150 |
setLnP(getLnP() + isNewLn);
|
|
|
151 |
setLastCellPosition(x, y, w, h, ln);
|
|
|
152 |
return this;
|
|
|
153 |
};
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Return an array containing all of the owned keys of an Object
|
|
|
157 |
* @type {Function}
|
|
|
158 |
* @return {String[]} of Object keys
|
|
|
159 |
*/
|
|
|
160 |
jsPDFAPI.getKeys = (typeof Object.keys === 'function')
|
|
|
161 |
? function (object) {
|
|
|
162 |
if (!object) {
|
|
|
163 |
return [];
|
|
|
164 |
}
|
|
|
165 |
return Object.keys(object);
|
|
|
166 |
}
|
|
|
167 |
: function (object) {
|
|
|
168 |
var keys = [],
|
|
|
169 |
property;
|
|
|
170 |
|
|
|
171 |
for (property in object) {
|
|
|
172 |
if (object.hasOwnProperty(property)) {
|
|
|
173 |
keys.push(property);
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
return keys;
|
|
|
178 |
};
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
* Return the maximum value from an array
|
|
|
182 |
* @param array
|
|
|
183 |
* @param comparisonFn
|
|
|
184 |
* @returns {*}
|
|
|
185 |
*/
|
|
|
186 |
jsPDFAPI.arrayMax = function (array, comparisonFn) {
|
|
|
187 |
var max = array[0],
|
|
|
188 |
i,
|
|
|
189 |
ln,
|
|
|
190 |
item;
|
|
|
191 |
|
|
|
192 |
for (i = 0, ln = array.length; i < ln; i += 1) {
|
|
|
193 |
item = array[i];
|
|
|
194 |
|
|
|
195 |
if (comparisonFn) {
|
|
|
196 |
if (comparisonFn(max, item) === -1) {
|
|
|
197 |
max = item;
|
|
|
198 |
}
|
|
|
199 |
} else {
|
|
|
200 |
if (item > max) {
|
|
|
201 |
max = item;
|
|
|
202 |
}
|
|
|
203 |
}
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
return max;
|
|
|
207 |
};
|
|
|
208 |
|
|
|
209 |
/**
|
|
|
210 |
* Create a table from a set of data.
|
|
|
211 |
* @param {Object[]} data As array of objects containing key-value pairs
|
|
|
212 |
* @param {String[]} [headers] Omit or null to auto-generate headers at a performance cost
|
|
|
213 |
* @param {Object} [config.printHeaders] True to print column headers at the top of every page
|
|
|
214 |
* @param {Object} [config.autoSize] True to dynamically set the column widths to match the widest cell value
|
|
|
215 |
* @param {Object} [config.autoStretch] True to force the table to fit the width of the page
|
|
|
216 |
*/
|
|
|
217 |
jsPDFAPI.table = function (data, headers, config) {
|
|
|
218 |
|
|
|
219 |
var headerNames = [],
|
|
|
220 |
headerPrompts = [],
|
|
|
221 |
header,
|
|
|
222 |
autoSize,
|
|
|
223 |
printHeaders,
|
|
|
224 |
autoStretch,
|
|
|
225 |
i,
|
|
|
226 |
ln,
|
|
|
227 |
columnMatrix = {},
|
|
|
228 |
columnWidths = {},
|
|
|
229 |
columnData,
|
|
|
230 |
column,
|
|
|
231 |
columnMinWidths = [],
|
|
|
232 |
j,
|
|
|
233 |
tableHeaderConfigs = [],
|
|
|
234 |
model,
|
|
|
235 |
jln,
|
|
|
236 |
func;
|
|
|
237 |
|
|
|
238 |
/**
|
|
|
239 |
* @property {Number} lnMod
|
|
|
240 |
* Keep track of the current line number modifier used when creating cells
|
|
|
241 |
*/
|
|
|
242 |
this.lnMod = 0;
|
|
|
243 |
|
|
|
244 |
if (config) {
|
|
|
245 |
autoSize = config.autoSize || false;
|
|
|
246 |
printHeaders = this.printHeaders = config.printHeaders || true;
|
|
|
247 |
autoStretch = config.autoStretch || true;
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
if (!data) {
|
|
|
251 |
throw 'No data for PDF table';
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
// Set headers
|
|
|
255 |
if (headers === undefined || (headers === null)) {
|
|
|
256 |
|
|
|
257 |
// No headers defined so we derive from data
|
|
|
258 |
headerNames = this.getKeys(data[0]);
|
|
|
259 |
|
|
|
260 |
} else if (headers[0] && (typeof headers[0] !== 'string')) {
|
|
|
261 |
|
|
|
262 |
// Split header configs into names and prompts
|
|
|
263 |
for (i = 0, ln = headers.length; i < ln; i += 1) {
|
|
|
264 |
header = headers[i];
|
|
|
265 |
headerNames.push(header.name);
|
|
|
266 |
headerPrompts.push(header.prompt);
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
} else {
|
|
|
270 |
headerNames = headers;
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
if (config.autoSize) {
|
|
|
274 |
|
|
|
275 |
// Create Columns Matrix
|
|
|
276 |
|
|
|
277 |
func = function (rec) {
|
|
|
278 |
return rec[header];
|
|
|
279 |
};
|
|
|
280 |
|
|
|
281 |
for (i = 0, ln = headerNames.length; i < ln; i += 1) {
|
|
|
282 |
header = headerNames[i];
|
|
|
283 |
|
|
|
284 |
columnMatrix[header] = data.map(
|
|
|
285 |
func
|
|
|
286 |
);
|
|
|
287 |
|
|
|
288 |
// get header width
|
|
|
289 |
columnMinWidths.push(this.getTextDimensions(headerPrompts[i] || header).w);
|
|
|
290 |
|
|
|
291 |
column = columnMatrix[header];
|
|
|
292 |
|
|
|
293 |
// get cell widths
|
|
|
294 |
for (j = 0, ln = column.length; j < ln; j += 1) {
|
|
|
295 |
columnData = column[j];
|
|
|
296 |
|
|
|
297 |
columnMinWidths.push(this.getTextDimensions(columnData).w);
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
// get final column width
|
|
|
301 |
columnWidths[header] = jsPDFAPI.arrayMax(columnMinWidths);
|
|
|
302 |
}
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
// -- Construct the table
|
|
|
306 |
|
|
|
307 |
if (config.printHeaders) {
|
|
|
308 |
|
|
|
309 |
// Construct the header row
|
|
|
310 |
for (i = 0, ln = headerNames.length; i < ln; i += 1) {
|
|
|
311 |
header = headerNames[i];
|
|
|
312 |
tableHeaderConfigs.push([10, 10, columnWidths[header], 25, String(headerPrompts.length ? headerPrompts[i] : header)]);
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
// Store the table header config
|
|
|
316 |
this.setTableHeaderRow(tableHeaderConfigs);
|
|
|
317 |
|
|
|
318 |
// Print the header for the start of the table
|
|
|
319 |
this.printHeaderRow(1);
|
|
|
320 |
}
|
|
|
321 |
|
|
|
322 |
// Construct the data rows
|
|
|
323 |
for (i = 0, ln = data.length; i < ln; i += 1) {
|
|
|
324 |
model = data[i];
|
|
|
325 |
|
|
|
326 |
for (j = 0, jln = headerNames.length; j < jln; j += 1) {
|
|
|
327 |
header = headerNames[j];
|
|
|
328 |
this.cell(10, 10, columnWidths[header], 25, String(model[header]), i + 2);
|
|
|
329 |
}
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
return this;
|
|
|
333 |
};
|
|
|
334 |
|
|
|
335 |
/**
|
|
|
336 |
* Store the config for outputting a table header
|
|
|
337 |
* @param {Object[]} config
|
|
|
338 |
* An array of cell configs that would define a header row: Each config matches the config used by jsPDFAPI.cell
|
|
|
339 |
* except the ln parameter is excluded
|
|
|
340 |
*/
|
|
|
341 |
jsPDFAPI.setTableHeaderRow = function (config) {
|
|
|
342 |
this.tableHeaderRow = config;
|
|
|
343 |
};
|
|
|
344 |
|
|
|
345 |
/**
|
|
|
346 |
* Output the store header row
|
|
|
347 |
* @param lineNumber The line number to output the header at
|
|
|
348 |
*/
|
|
|
349 |
jsPDFAPI.printHeaderRow = function (lineNumber) {
|
|
|
350 |
if (!this.tableHeaderRow) {
|
|
|
351 |
throw 'Property tableHeaderRow does not exist.';
|
|
|
352 |
}
|
|
|
353 |
|
|
|
354 |
var tableHeaderCell,
|
|
|
355 |
tmpArray,
|
|
|
356 |
i,
|
|
|
357 |
ln;
|
|
|
358 |
|
|
|
359 |
this.printingHeaderRow = true;
|
|
|
360 |
|
|
|
361 |
for (i = 0, ln = this.tableHeaderRow.length; i < ln; i += 1) {
|
|
|
362 |
|
|
|
363 |
tableHeaderCell = this.tableHeaderRow[i];
|
|
|
364 |
tmpArray = [].concat(tableHeaderCell);
|
|
|
365 |
|
|
|
366 |
this.cell.apply(this, tmpArray.concat(lineNumber));
|
|
|
367 |
}
|
|
|
368 |
|
|
|
369 |
this.printingHeaderRow = false;
|
|
|
370 |
};
|
|
|
371 |
|
|
|
372 |
}(jsPDF.API));
|