Export HTML to PDF
using jsPDF and jsPDFautoTable
by yeyene
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.4.1/jspdf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.3.5/jspdf.plugin.autotable.min.js"></script>
<button>Export PDF</button><br><br>
<table class="table" id="example">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
JavaScript
$('button').on('click', function() {
var w = $('#tbl_user .t-Region-body').outerWidth();
var h = $('#tbl_user .t-Region-body').outerHeight();
if (w > 1170) w = w;
else w = 1170;
if (h > 825) h = h;
else h = 825;
$("#example").tableHTMLExport({
type: 'pdf',
filename: 'sample.pdf',
dimension: [w, h]
});
});
(function($){
$.fn.extend({
tableHTMLExport: function(options) {
//https://artskydj.github.io/jsPDF/docs/jsPDF.html
var defaults = {
separator: ',',
newline: '\r\n',
ignoreColumns: '',
ignoreRows: '',
type:'csv',
htmlContent: false,
consoleLog: false,
trimContent: true,
quoteFields: true,
filename: 'tableHTMLExport.csv',
dimension: ''
};
var options = $.extend(defaults, options);
function quote(text) {
return '"' + text.replace('"', '""') + '"';
}
function parseString(data){
if(defaults.htmlContent){
content_data = data.html().trim();
}else{
content_data = data.text().trim();
}
return content_data;
}
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
/**
* Convierte la tabla enviada a json
* @param el
* @returns {{header: *, data:...