Skip to content

Commit a7de195

Browse files
committed
jspdf.plugin.cell.js :
-Fixed some issues that broke cell fonctionnality. -Added Opera and Safari browser check when saving pdf. jspdf.js : -Fixed Blob creation params.
1 parent aadc92c commit a7de195

2 files changed

Lines changed: 74 additions & 60 deletions

File tree

jspdf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,7 @@ function jsPDF(/** String */ orientation, /** String */ unit, /** String */ form
17081708
array[i] = data.charCodeAt(i);
17091709
}
17101710

1711-
var blob = new Blob(array, {type: "application/pdf"});
1711+
var blob = new Blob([array], {type: "application/pdf"});
17121712

17131713
saveAs(blob, options);
17141714
break;

jspdf.plugin.cell.js

Lines changed: 73 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
*/
2525

2626
(function (jsPDFAPI) {
27-
// 'use strict';
27+
'use strict';
28+
/*jslint browser:true */
29+
/*global document: false */
30+
/*predef (jsPDF) */
2831

2932
var maxLn = 0,
3033
lnP = 0,
@@ -64,7 +67,7 @@
6467

6568
text = document.createElement('font');
6669
text.id = "jsPDFCell";
67-
text.style = "font-family: ' + fontName + ';font-size:' + fontSize + 'pt;font-style: ' + fontStyle + ';";
70+
// error text.style = "font-family: ' + fontName + ';font-size:' + fontSize + 'pt;font-style: ' + fontStyle + ';";
6871
text.innerText = txt;
6972

7073
document.body.appendChild(text);
@@ -93,28 +96,29 @@
9396
};
9497

9598
jsPDFAPI.cell = function (x, y, w, h, txt, ln) {
96-
if(this.printingHeaderRow !== true && this.lnMod !== 0){
99+
this.lnMod = this.lnMod === undefined ? 0 : this.lnMod;
100+
if (this.printingHeaderRow !== true && this.lnMod !== 0) {
97101
ln = ln + this.lnMod;
98102
}
99103

100104
if ((((ln * h) + y + (h * 2)) / pages) >= this.internal.pageSize.height && pages === 1 && !newPage) {
101105
this.cellAddPage();
102106

103-
if(this.printHeaders && this.tableHeaderRow){
107+
if (this.printHeaders && this.tableHeaderRow) {
104108
this.printHeaderRow(ln);
105-
this.lnMod++;
106-
ln++;
109+
this.lnMod += 1;
110+
ln += 1;
107111
}
108112
if (getMaxLn() === 0) {
109113
setMaxLn(Math.round((this.internal.pageSize.height - (h * 2)) / h));
110114
}
111115
} else if (newPage && getLastCellPosition().ln !== ln && getLnP() === getMaxLn()) {
112116
this.cellAddPage();
113117

114-
if(this.printHeaders && this.tableHeaderRow){
118+
if (this.printHeaders && this.tableHeaderRow) {
115119
this.printHeaderRow(ln);
116-
this.lnMod++;
117-
ln++;
120+
this.lnMod += 1;
121+
ln += 1;
118122
}
119123
}
120124

@@ -151,14 +155,14 @@
151155
* @type {Function}
152156
* @return {String[]} of Object keys
153157
*/
154-
jsPDFAPI.getKeys = (typeof Object.keys == 'function')
155-
? function(object){
158+
jsPDFAPI.getKeys = (typeof Object.keys === 'function')
159+
? function (object) {
156160
if (!object) {
157161
return [];
158162
}
159163
return Object.keys(object);
160164
}
161-
: function(object) {
165+
: function (object) {
162166
var keys = [],
163167
property;
164168

@@ -177,19 +181,20 @@
177181
* @param comparisonFn
178182
* @returns {*}
179183
*/
180-
jsPDFAPI.arrayMax = function(array, comparisonFn) {
184+
jsPDFAPI.arrayMax = function (array, comparisonFn) {
181185
var max = array[0],
182-
i, ln, item;
186+
i,
187+
ln,
188+
item;
183189

184-
for (i = 0, ln = array.length; i < ln; i++) {
190+
for (i = 0, ln = array.length; i < ln; i += 1) {
185191
item = array[i];
186192

187193
if (comparisonFn) {
188194
if (comparisonFn(max, item) === -1) {
189195
max = item;
190196
}
191-
}
192-
else {
197+
} else {
193198
if (item > max) {
194199
max = item;
195200
}
@@ -207,36 +212,52 @@
207212
* @param {Object} [config.autoSize] True to dynamically set the column widths to match the widest cell value
208213
* @param {Object} [config.autoStretch] True to force the table to fit the width of the page
209214
*/
210-
jsPDFAPI.table = function(data, headers, config){
211-
212-
var headerNames = [], headerPrompts = [], header;
215+
jsPDFAPI.table = function (data, headers, config) {
216+
217+
var headerNames = [],
218+
headerPrompts = [],
219+
header,
220+
autoSize,
221+
printHeaders,
222+
autoStretch,
223+
i,
224+
ln,
225+
columnMatrix = {},
226+
columnWidths = {},
227+
columnData,
228+
column,
229+
columnMinWidths = [],
230+
j,
231+
tableHeaderConfigs = [],
232+
model,
233+
jln;
213234

214235
/**
215236
* @property {Number} lnMod
216237
* Keep track of the current line number modifier used when creating cells
217238
*/
218239
this.lnMod = 0;
219240

220-
if(config){
221-
var autoSize = config.autoSize || false,
222-
printHeaders = this.printHeaders = config.printHeaders || true,
223-
autoStretch = config.autoStretch || true;
241+
if (config) {
242+
autoSize = config.autoSize || false;
243+
printHeaders = this.printHeaders = config.printHeaders || true;
244+
autoStretch = config.autoStretch || true;
224245
}
225246

226-
if(!data){
227-
throw 'No data for PDF table';
247+
if (!data) {
248+
throw 'No data for PDF table';
228249
}
229250

230251
// Set headers
231-
if(headers === undefined || (headers === null)){
252+
if (headers === undefined || (headers === null)) {
232253

233254
// No headers defined so we derive from data
234255
headerNames = this.getKeys(data[0]);
235256

236-
} else if (headers[0] && !(typeof headers[0] == 'string')) {
257+
} else if (headers[0] && (typeof headers[0] !== 'string')) {
237258

238259
// Split header configs into names and prompts
239-
for (var i = 0, ln = headers.length; i < ln; i++) {
260+
for (i = 0, ln = headers.length; i < ln; i += 1) {
240261
header = headers[i];
241262
headerNames.push(header.name);
242263
headerPrompts.push(header.prompt);
@@ -246,30 +267,26 @@
246267
headerNames = headers;
247268
}
248269

249-
if(config.autoSize){
270+
if (config.autoSize) {
250271

251272
// Create Columns Matrix
252-
var columnMatrix = {},
253-
columnWidths = {},
254-
columnData,
255-
column;
256273

257-
for (var i = 0, ln = headerNames.length; i < ln; i++) {
274+
for (i = 0, ln = headerNames.length; i < ln; i += 1) {
258275
header = headerNames[i];
259276

260-
columnMatrix[header] = data.map(function(rec){
261-
return rec[header];
262-
});
263-
264-
var columnMinWidths = [];
277+
columnMatrix[header] = data.map(
278+
function (rec) {
279+
return rec[header];
280+
}
281+
);
265282

266283
// get header width
267-
columnMinWidths.push(this.getTextDimensions(headerPrompts[i]||header).w);
284+
columnMinWidths.push(this.getTextDimensions(headerPrompts[i] || header).w);
268285

269286
column = columnMatrix[header];
270287

271288
// get cell widths
272-
for (var j = 0, ln = column.length; j < ln; j++) {
289+
for (j = 0, ln = column.length; j < ln; j += 1) {
273290
columnData = column[j];
274291

275292
columnMinWidths.push(this.getTextDimensions(columnData).w);
@@ -282,13 +299,10 @@
282299

283300
// -- Construct the table
284301

285-
if (config.printHeaders){
302+
if (config.printHeaders) {
286303

287304
// Construct the header row
288-
var tableHeaderConfigs = [];
289-
290-
291-
for (var i = 0, ln = headerNames.length; i < ln; i++) {
305+
for (i = 0, ln = headerNames.length; i < ln; i += 1) {
292306
header = headerNames[i];
293307
tableHeaderConfigs.push([10, 10, columnWidths[header], 25, String(headerPrompts.length ? headerPrompts[i] : header)]);
294308
}
@@ -301,14 +315,12 @@
301315
}
302316

303317
// Construct the data rows
304-
var model;
305-
306-
for (var i = 0, ln = data.length; i < ln; i++) {
318+
for (i = 0, ln = data.length; i < ln; i += 1) {
307319
model = data[i];
308320

309-
for (var j = 0, jln = headerNames.length; j < jln; j++) {
321+
for (j = 0, jln = headerNames.length; j < jln; j += 1) {
310322
header = headerNames[j];
311-
this.cell(10, 10, columnWidths[header], 25, String(model[header]), i+2);
323+
this.cell(10, 10, columnWidths[header], 25, String(model[header]), i + 2);
312324
}
313325
}
314326

@@ -321,25 +333,27 @@
321333
* An array of cell configs that would define a header row: Each config matches the config used by jsPDFAPI.cell
322334
* except the ln parameter is excluded
323335
*/
324-
jsPDFAPI.setTableHeaderRow = function(config){
336+
jsPDFAPI.setTableHeaderRow = function (config) {
325337
this.tableHeaderRow = config;
326338
};
327339

328340
/**
329341
* Output the store header row
330342
* @param lineNumber The line number to output the header at
331343
*/
332-
jsPDFAPI.printHeaderRow = function(lineNumber){
333-
334-
if (!this.tableHeaderRow){
344+
jsPDFAPI.printHeaderRow = function (lineNumber) {
345+
if (!this.tableHeaderRow) {
335346
throw 'Property tableHeaderRow does not exist.';
336347
}
337348

338-
var tableHeaderCell, tmpArray;
349+
var tableHeaderCell,
350+
tmpArray,
351+
i,
352+
ln;
339353

340354
this.printingHeaderRow = true;
341355

342-
for (var i = 0, ln = this.tableHeaderRow.length; i < ln; i++) {
356+
for (i = 0, ln = this.tableHeaderRow.length; i < ln; i += 1) {
343357

344358
tableHeaderCell = this.tableHeaderRow[i];
345359
tmpArray = [].concat(tableHeaderCell);
@@ -350,4 +364,4 @@
350364
this.printingHeaderRow = false;
351365
};
352366

353-
})(jsPDF.API);
367+
}(jsPDF.API));

0 commit comments

Comments
 (0)