Skip to content

Commit e3bbbb9

Browse files
author
hhurz
committed
pdf: draw cell text with original <td> alignment
1 parent 4fb4842 commit e3bbbb9

File tree

2 files changed

+64
-28
lines changed

2 files changed

+64
-28
lines changed

tableExport.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,11 @@ THE SOFTWARE.*/
475475

476476
$(el).filter(':visible').each(function() {
477477
if ($(this).css('display') != 'none') {
478-
var headers = [];
479-
var rows = [];
480478
var rowIndex = 0;
481479
var atOptions = {};
480+
481+
teOptions.columns = [];
482+
teOptions.rows = [];
482483

483484
// onTable: optional callback function for every matching table that can be used
484485
// to modify the tableExport options or to skip the output of a particular table
@@ -494,12 +495,44 @@ THE SOFTWARE.*/
494495
atOptions.margins = {};
495496
$.extend(true, atOptions.margins, defaults.jspdf.margins);
496497

498+
if (typeof atOptions.renderCell !== 'function') {
499+
500+
// draw cell text with original <td> alignment
501+
atOptions.renderCell = function (x, y, width, height, key, value, row, settings)
502+
{
503+
var doc = settings.tableExport.doc;
504+
var col = settings.tableExport.columns [key];
505+
var xoffset = 0;
506+
507+
doc.setFillColor(row % 2 === 0 ? 245 : 255);
508+
doc.setTextColor(50);
509+
doc.rect(x, y, width, height, 'F');
510+
y += settings.lineHeight / 2 + doc.autoTableTextHeight() / 2 - 2.5;
511+
512+
if (col.style.align == 'right')
513+
xoffset = width - doc.getStringUnitWidth((''+value)) * doc.internal.getFontSize() - settings.padding;
514+
else if (col.style.align == 'center')
515+
xoffset = (width - doc.getStringUnitWidth((''+value)) * doc.internal.getFontSize()) / 2;
516+
517+
if (xoffset < settings.padding)
518+
xoffset = settings.padding;
519+
520+
doc.text(value, x + xoffset, y);
521+
}
522+
}
523+
497524
// collect header and data rows
498525
$(this).find('thead').find(defaults.theadSelector).each(function() {
499-
526+
500527
ForEachVisibleCell(this, 'th,td', rowIndex,
501528
function(cell, row, col) {
502-
headers.push(parseString(cell, row, col));
529+
var obj = {title: parseString(cell, row, col),
530+
key: col,
531+
style: {align: getStyle(cell, 'text-align'),
532+
bcolor: getStyle(cell, 'background-color')
533+
}
534+
};
535+
teOptions.columns.push (obj);
503536
});
504537
rowIndex++;
505538
});
@@ -512,15 +545,15 @@ THE SOFTWARE.*/
512545
rowData.push(parseString(cell, row, col));
513546
});
514547
rowIndex++;
515-
rows.push(rowData);
548+
teOptions.rows.push(rowData);
516549
});
517550

518551
// onBeforeAutotable: optional callback function before calling
519552
// jsPDF AutoTable that can be used to modify the AutoTable options
520553
if (typeof teOptions.onBeforeAutotable === 'function')
521-
teOptions.onBeforeAutotable($(this), headers, rows, atOptions);
554+
teOptions.onBeforeAutotable($(this), teOptions.columns, teOptions.rows, atOptions);
522555

523-
teOptions.doc.autoTable(headers, rows, atOptions);
556+
teOptions.doc.autoTable(teOptions.columns, teOptions.rows, atOptions);
524557

525558
// onAfterAutotable: optional callback function after returning
526559
// from jsPDF AutoTable that can be used to modify the AutoTable options
@@ -534,6 +567,8 @@ THE SOFTWARE.*/
534567

535568
jsPdfOutput(teOptions.doc);
536569

570+
teOptions.columns.length = 0;
571+
teOptions.rows.length = 0;
537572
delete teOptions.doc;
538573
teOptions.doc = null;
539574
}

0 commit comments

Comments
 (0)