jsPDF AutoTable rowspan/colspan from HTML table

riwanab picture riwanab · Nov 8, 2015 · Viewed 11k times · Source

I'm using jsPDF AutoTable (https://github.com/simonbengtsson/jsPDF-AutoTable).

I have an HTML table with rowspan="2" and when the pdf is generated with this table, there is no rowspan="2" anymore :(

I modify the plugin to add rowSpan attribute (see [[[[[ ADDED ]]]]] mark ) :

var autoTableHtmlToJson = function(table){

    var data = [],
        headers = [],
        header = table.rows[0],
        tableRow,
        rowData,
        i,
        j;

    for( i = 0 ; i < header.cells.length;i++){
        headers.push( (typeof header.cells[i] !== "undefined") ? header.cells[i].textContent : "" );
    }

    for( i = 1 ; i < table.rows.length; i++ ){
        tableRow = table.rows[i];
        rowData = [];

        for( j = 0; j < header.cells.length; j++ ){
            if( typeof tableRow.cells[j] !== "undefined" ){
                rowData.push({
                    value   : tableRow.cells[j].textContent,
                    rowSpan : tableRow.cells[j].rowSpan <==== [[[[[ ADDED ]]]]]
                });
            }
            else{
                rowData.push({
                    value   : "",
                    rowSpan : 1 <==== [[[[[ ADDED ]]]]]
                });
            }
        }
        data.push( rowData );
    }
    return { columns:headers, data:data, rows:data }
};

Then for each cell to draw I have :

    drawCell: function (cell, data) {
                    doc.rect(cell.x, cell.y, cell.width, cell.height * cell.raw.rowSpan, 'S');
                    doc.autoTableText(cell.raw.value,cell.x + cell.width / 2, cell.y + cell.height * cell.raw.rowSpan, {
                    halign: 'center',
                    valign: 'middle',
                    overflow: 'linebreak'
                });
                return false;

                }

The rowSpan has the right value in drawCell but the pdf result is very wrong ! The pdf have all data on the same line and I don't know why. Sorry I can't post the screenshot because I have not the reputation for.

Answer

Erik Hellquist picture Erik Hellquist · Dec 3, 2015

EDIT: Rowspans, colspans and css styling are now supported and should work out of the box docs

The jspdf-autotable plugin currently only supports converting data from an html table. All styling including rowspans etc have to be applied manually.