Skip to content

Commit

Permalink
- Release 1.10.22
Browse files Browse the repository at this point in the history
- Added some more pdfmake options
- Fixed a minor issue addressing XLSX export of hyperlinks
  • Loading branch information
hhurz committed Mar 21, 2021
1 parent 8844eac commit ac867b5
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 110 deletions.
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,26 @@ To export the table in XLSX (Excel 2007+ XML Format) format, you need to include
<script type="text/javascript" src="libs/js-xlsx/xlsx.core.min.js"></script>
```

To export the table as a PDF file the following includes are required if using jsPDF and jsPDF-AutoTable as pdf producer:
To export an html table to a PDF file, you can use jsPDF-AutoTable as a PDF producer:

```html
<script type="text/javascript" src="libs/jsPDF/jspdf.min.js"></script>
<script type="text/javascript" src="libs/jsPDF-AutoTable/jspdf.plugin.autotable.js"></script>
```

As an alternative pdf producer you can use pdfmake. Instead of jsPDF include then
Many HTML stylings can be converted to PDF with jsPDF, but support for non-western character sets is almost non-existent. Especially if you want to export Arabic or Chinese characters to your PDF file, you can use pdfmake as an alternative PDF producer. The disadvantage compared to jspdf is that using pdfmake has a reduced styling capability. To use pdfmake enable the pdfmake option and instead of the jsPDF files include

```html
<script type="text/javascript" src="libs/pdfmake/pdfmake.min.js"></script>
<script type="text/javascript" src="libs/pdfmake/vfs_fonts.js"></script>
<!-- For output of arabic characters include mirza_fonts.js instead of vfs_fonts.js -->
<!-- For output of chinese characters include either gbsn00lp_fonts.js or ZCOOLXiaoWei_fonts.js instead of vfs_fonts.js -->

<!-- To export arabic characters include mirza_fonts.js _instead_ of vfs_fonts.js
<script type="text/javascript" src="libs/pdfmake/mirza_fonts.js"></script>
-->

<!-- For a chinese font include either gbsn00lp_fonts.js or ZCOOLXiaoWei_fonts.js _instead_ of vfs_fonts.js
<script type="text/javascript" src="libs/pdfmake/gbsn00lp_fonts.js"></script>
-->
```

To export the table in PNG format, you need to include:
Expand Down Expand Up @@ -224,8 +230,17 @@ onTableExportBegin: null
onTableExportEnd: null
outputMode: 'file'
pdfmake: enabled: false
docDefinition: pageOrientation: 'portrait'
defaultStyle: font: 'Roboto'
docDefinition: pageSize: 'A4'
pageOrientation: 'portrait'
styles: header: background: '#34495E'
color: '#FFFFFF'
bold: true
alignment: 'center'
fillColor: '#34495E
alternateRow: fillColor: '#f5f5f5'
defaultStyle: color: '#000000'
fontSize: 8
font: 'Roboto'
fonts: {}
preserve: leadingWS: false
trailingWS: false
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tableexport.jquery.plugin",
"version": "1.10.21",
"version": "1.10.22",
"description": "html table export",
"main": "tableExport.js",
"authors": [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tableexport.jquery.plugin",
"version": "1.10.21",
"version": "1.10.22",
"description": "html table export",
"main": "tableExport.min.js",
"dependencies": {
Expand Down
98 changes: 70 additions & 28 deletions tableExport.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* @preserve tableExport.jquery.plugin
*
* Version 1.10.21
* Version 1.10.22
*
* Copyright (c) 2015-2020 hhurz, https://github.com/hhurz/tableExport.jquery.plugin
* Copyright (c) 2015-2021 hhurz, https://github.com/hhurz/tableExport.jquery.plugin
*
* Based on https://github.com/kayalshri/tableExport.jquery.plugin
*
Expand Down Expand Up @@ -107,11 +107,26 @@
pdfmake: {
enabled: false, // true: Use pdfmake as pdf producer instead of jspdf and jspdf-autotable
docDefinition: {
pageSize: 'A4', // 4A0,2A0,A{0-10},B{0-10},C{0-10},RA{0-4},SRA{0-4},EXECUTIVE,FOLIO,LEGAL,LETTER,TABLOID
pageOrientation: 'portrait', // 'portrait' or 'landscape'
styles: {
header: {
background: '#34495E',
color: '#FFFFFF',
bold: true,
alignment: 'center',
fillColor: '#34495E'
},
alternateRow: {
fillColor: '#f5f5f5'
}
},
defaultStyle: {
font: 'Roboto' // Default font is 'Roboto' (needs vfs_fonts.js to be included)
} // For an arabic font include mirza_fonts.js instead of vfs_fonts.js
}, // For a chinese font include either gbsn00lp_fonts.js or ZCOOLXiaoWei_fonts.js instead of vfs_fonts.js
color: '#000000',
fontSize: 8,
font: 'Roboto' // Default font is 'Roboto' which needs vfs_fonts.js to be included
} // To export arabic characters include mirza_fonts.js _instead_ of vfs_fonts.js
}, // For a chinese font include either gbsn00lp_fonts.js or ZCOOLXiaoWei_fonts.js _instead_ of vfs_fonts.js
fonts: {}
},
preserve: {
Expand Down Expand Up @@ -942,23 +957,31 @@

ForEachVisibleCell(this, colselector, rowIndex, length,
function (cell, row, col) {
var cellContent;

if (typeof cell !== 'undefined' && cell !== null) {

var colspan = getColspan(cell);
var rowspan = getRowspan(cell);

var cellValue = parseString(cell, row, col) || ' ';
cellContent = {text: parseString(cell, row, col) || ' '};

if (colspan > 1 || rowspan > 1) {
colspan = colspan || 1;
rowspan = rowspan || 1;
r.push({colSpan: colspan, rowSpan: rowspan, text: cellValue});
} else
r.push(cellValue);
} else
r.push(' ');
cellContent['colSpan'] = colspan || 1;
cellContent['rowSpan'] = rowspan || 1;
}
}
else
cellContent = {text: ' '};

if (colselector.indexOf('th') >= 0)
cellContent['style'] = 'header';

r.push(cellContent);
});

for (var i = r.length; i < length; i++)
r.push('');

if (r.length)
body.push(r);

Expand All @@ -981,14 +1004,31 @@
// Data
$rows = collectRows($table);

CollectPdfmakeData($rows, 'th,td', $hrows.length + $rows.length);
colcount = CollectPdfmakeData($rows, 'td', $hrows.length + $rows.length);

for (var i = widths.length; i < colcount; i++)
widths.push('*');

docDefinition.content.push({ table: {
headerRows: $hrows.length,
headerRows: $hrows.length ? $hrows.length : null,
widths: widths,
body: body
},
pageBreak: docDefinition.content.length ? "before" : undefined
},
layout: {
layout: 'noBorders',
hLineStyle: function (i, node) { return 0; },
vLineWidth: function (i, node) { return 0; },
hLineColor: function (i, node) { return i < node.table.headerRows ?
defaults.pdfmake.docDefinition.styles.header.background :
defaults.pdfmake.docDefinition.styles.alternateRow.fillColor; },
vLineColor: function (i, node) { return i < node.table.headerRows ?
defaults.pdfmake.docDefinition.styles.header.background :
defaults.pdfmake.docDefinition.styles.alternateRow.fillColor; },
fillColor: function (rowIndex, node, columnIndex) { return (rowIndex % 2 === 0) ?
defaults.pdfmake.docDefinition.styles.alternateRow.fillColor :
null; }
},
pageBreak: docDefinition.content.length ? "before" : undefined
});
}); // ...for each table

Expand All @@ -1004,23 +1044,23 @@
};

if (pdfMake.vfs.hasOwnProperty ('Mirza-Regular.ttf')) {
defaults.pdfmake.docDefinition.defaultStyle.font = 'Mirza';
docDefinition.defaultStyle.font = 'Mirza';
$.extend(true, pdfMake.fonts, {Mirza: {normal: 'Mirza-Regular.ttf',
bold: 'Mirza-Bold.ttf',
italics: 'Mirza-Medium.ttf',
bolditalics: 'Mirza-SemiBold.ttf'
}});
}
else if (pdfMake.vfs.hasOwnProperty ('gbsn00lp.ttf')) {
defaults.pdfmake.docDefinition.defaultStyle.font = 'gbsn00lp';
docDefinition.defaultStyle.font = 'gbsn00lp';
$.extend(true, pdfMake.fonts, {gbsn00lp: {normal: 'gbsn00lp.ttf',
bold: 'gbsn00lp.ttf',
italics: 'gbsn00lp.ttf',
bolditalics: 'gbsn00lp.ttf'
}});
}
else if (pdfMake.vfs.hasOwnProperty ('ZCOOLXiaoWei-Regular.ttf')) {
defaults.pdfmake.docDefinition.defaultStyle.font = 'ZCOOLXiaoWei';
docDefinition.defaultStyle.font = 'ZCOOLXiaoWei';
$.extend(true, pdfMake.fonts, {ZCOOLXiaoWei: {normal: 'ZCOOLXiaoWei-Regular.ttf',
bold: 'ZCOOLXiaoWei-Regular.ttf',
italics: 'ZCOOLXiaoWei-Regular.ttf',
Expand Down Expand Up @@ -2294,7 +2334,7 @@
var v = parseString(elt, _R, _C + CSOffset, cellInfo);
var o = {t: 's', v: v};
var _t = '';
var cellFormat = $(elt).attr('data-tableexport-cellformat');
var cellFormat = $(elt).attr('data-tableexport-cellformat') || '';

if (cellFormat !== '') {
var ssfId = parseInt($(elt).attr('data-tableexport-xlsxformatid') || 0);
Expand Down Expand Up @@ -2323,18 +2363,20 @@

if (v.length === 0)
o.t = 'z';
else if (v.trim().length === 0 || _t === 's') {
else if (v.trim().length === 0) {
}
else if (_t === 's') {
if ($(elt).find('a').length) {
v = defaults.htmlHyperlink !== 'href' ? v : '';
o = {f: '=HYPERLINK("' + $(elt).find('a').attr('href') + (v.length ? '","' + v : '') + '")'};
}
}
else if (cellInfo.type === 'function')
o = {f: v};
else if (v === 'TRUE')
o = {t: 'b', v: true};
else if (v === 'FALSE')
o = {t: 'b', v: false};
else if (_t === '' && $(elt).find('a').length) {
v = defaults.htmlHyperlink !== 'href' ? v : '';
o = {f: '=HYPERLINK("' + $(elt).find('a').attr('href') + (v.length ? '","' + v : '') + '")'};
}
else if (_t === 'n' || isFinite(xlsxToNumber(v, defaults.numbers.output))) { // yes, defaults.numbers.output is right
var vn = xlsxToNumber(v, defaults.numbers.output);
if (ssfId === 0 && typeof defaults.mso.xslx.formatId.numbers !== 'function')
Expand Down
Loading

0 comments on commit ac867b5

Please sign in to comment.