Saturday, March 9, 2024

Export to PDF with javascript


<script>

/* function exportexcel(){

//getting data from our div that contains the HTML table

/* var data_type = 'data:application/vnd.ms-excel';

var table_div = document.getElementById('skillreport1');

var table_html = table_div.outerHTML.replace(/ /g, '%20');

window.open(data_type + ', ' + table_html);

} */

$(document).ready(function() {

    $("#btnExport").click(function(e) {

        //getting values of current time for generating the file name

        var dt = new Date();

        var day = dt.getDate();

        var month = dt.getMonth() + 1;

        var year = dt.getFullYear();

        var hour = dt.getHours();

        var mins = dt.getMinutes();

        var postfix = day + "." + month + "." + year + "_" + hour + "." + mins;

        //creating a temporary HTML link element (they support setting file names)

        var a = document.createElement('a');

document.body.appendChild(a);

        //getting data from our div that contains the HTML table

        var data_type = 'data:application/vnd.ms-excel';

        var table_div = document.getElementById('skillreport');

        var table_html = table_div.outerHTML.replace(/ /g, '%20');

        a.href = data_type + ', ' + table_html;

        //setting the file name

        a.download = 'exported_table_' + postfix + '.xls';

        //triggering the function

        a.click();

        //just in case, prevent default behaviour

        e.preventDefault();

    });

});

No comments:

Post a Comment