Create pdf using jsPDF with formatted Table data

user3428816 picture user3428816 · Apr 11, 2014 · Viewed 85.5k times · Source

I am able to generated PDF file from html table using this below script: But I am getting all the columns data are line by line.

Please help me to generate PDF file as a tabular formatted way.(with column border, margin or padding, headers ) in this script

I am used jsPDF lib script to generate a html table to PDF .

 var pdf = new jsPDF('p', 'pt', 'letter')   
    , source = $('#TableId')[0] 
    , specialElementHandlers = {
        // element with id of "bypass" - jQuery style selector
        '#bypassme': function(element, renderer){           
            return true
        }
    }

    , margins = {
             top: 20,
             bottom: 20,
             left: 30,
             width: 922
         };


    pdf.fromHTML(
        source // HTML string or DOM elem ref.
        , margins.left // x coord
        , margins.top // y coord
        , {
            'width': margins.width // max width of content on PDF
            , 'elementHandlers': specialElementHandlers
        },
        function (dispose) {          
          pdf.save('Test.pdf');
        },
        margins
    )

EDIT:

I have tried this sample below function too, but I am getting just empty pdf file.

function exportTabletoPdf()
{
    var doc = new jsPDF('p','pt', 'a4', true);
    var header = [1,2,3,4];
    doc.table(10, 10, $('#test').get(0), header, {
    left:10,
    top:10,
    bottom: 10,
    width: 170,
    autoSize:false,
    printHeaders: true
    });
    doc.save('sample-file.pdf');
}

Answer

Oscar Acevedo picture Oscar Acevedo · Oct 26, 2015

I spent a lot of time looking for a good representation of my tables, then I found this plugin (https://github.com/simonbengtsson/jsPDF-AutoTable), It works great, includes themes, rowspan, colspan, extract data from html, works with json, you can also personalize your headers and make them horizontals. The image below is an example: jsPDF-AutoTable