Skip to content

Commit

Permalink
PDF format (using pdfmake): Simplified support of Arabic and Chinese …
Browse files Browse the repository at this point in the history
…characters
  • Loading branch information
hhurz committed Jun 21, 2020
1 parent 07ee612 commit a717548
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 73 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,22 @@ 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:
To export the table as a PDF file the following includes are required if using jsPDF and jsPDF-AutoTable as 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

```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 the table in PNG format, you need to include:

```html
Expand Down Expand Up @@ -65,8 +74,9 @@ Library | Version
[es6-promise](https://github.com/stefanpenner/es6-promise) | \>= 4.2.4
[FileSaver](https://github.com/hhurz/tableExport.jquery.plugin/blob/master/libs/FileSaver/FileSaver.min.js) | \>= 1.2.0
[html2canvas](https://github.com/niklasvh/html2canvas) | \>= 0.5.0-beta4
[jsPDF](https://github.com/MrRio/jsPDF) | 1.3.2 - 1.3.4
[jsPDF](https://github.com/MrRio/jsPDF) | \>=1.3.4
[jsPDF-AutoTable](https://github.com/simonbengtsson/jsPDF-AutoTable) | 2.0.14 or 2.0.17
[pdfmake](https://github.com/bpampuch/pdfmake) | 0.1.65
[SheetJS](https://github.com/SheetJS/js-xlsx) | \>= 0.12.5


Expand Down Expand Up @@ -136,6 +146,15 @@ $('table').tableExport({fileName: sFileName,
});
```

```
// PDF export using pdfmake
$('#tableID').tableExport({type:'pdf',
pdfmake:{enabled:true,
docDefinition:{pageOrientation:'landscape'}}
});
```

Options (Default settings)
=======

Expand Down
60 changes: 44 additions & 16 deletions tableExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@
docDefinition: {
pageOrientation: 'portrait', // 'portrait' or 'landscape'
defaultStyle: {
font: 'Roboto' // Default is 'Roboto', for an arabic font set this option to 'Mirza' and include mirza_fonts.js
}
},
font: 'Roboto' // Default font is 'Roboto' (contained in vfs_fonts.js)
} // 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 instead of vfs_fonts.js
fonts: {}
},
preserve: {
Expand Down Expand Up @@ -981,23 +981,51 @@
}]
};

$.extend(true, docDefinition, defaults.pdfmake.docDefinition);
if (typeof pdfMake !== 'undefined') {

pdfMake.fonts = {
Roboto: {
normal: 'Roboto-Regular.ttf',
bold: 'Roboto-Medium.ttf',
italics: 'Roboto-Italic.ttf',
bolditalics: 'Roboto-MediumItalic.ttf'
pdfMake.fonts = {
Roboto: {
normal: 'Roboto-Regular.ttf',
bold: 'Roboto-Medium.ttf',
italics: 'Roboto-Italic.ttf',
bolditalics: 'Roboto-MediumItalic.ttf'
}
};

if (pdfMake.vfs.hasOwnProperty ('Mirza-Regular.ttf')) {
defaults.pdfmake.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';
$.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';
$.extend(true, pdfMake.fonts, {ZCOOLXiaoWei: {normal: 'ZCOOLXiaoWei-Regular.ttf',
bold: 'ZCOOLXiaoWei-Regular.ttf',
italics: 'ZCOOLXiaoWei-Regular.ttf',
bolditalics: 'ZCOOLXiaoWei-Regular.ttf'
}});
}
};

$.extend(true, pdfMake.fonts, defaults.pdfmake.fonts);

pdfMake.createPdf(docDefinition).getBuffer(function (buffer) {
saveToFile(buffer, defaults.fileName + '.pdf', 'application/pdf', '', '', false);
});
$.extend(true, docDefinition, defaults.pdfmake.docDefinition);
$.extend(true, pdfMake.fonts, defaults.pdfmake.fonts);

if (typeof pdfMake.createPdf !== 'undefined') {
pdfMake.createPdf(docDefinition).getBuffer(function (buffer) {
saveToFile(buffer, defaults.fileName + '.pdf', 'application/pdf', '', '', false);
});
}
}
} else if (defaults.jspdf.autotable === false) {
// pdf output using jsPDF's core html support

Expand Down
Loading

0 comments on commit a717548

Please sign in to comment.