jsPDF icon indicating copy to clipboard operation
jsPDF copied to clipboard

html table with hidden column

Open ghost opened this issue 12 years ago • 5 comments

Hi! Does the jsPDF does not support exporting html table with hidden column? It seems that only the data can be hidden in the table and if the headers are hidden the whole table will only show the ones with the header. I have been looking around for answers but then I only found none. This is an example of a hidden column. http://jsfiddle.net/riichan/1f8pbpg8/

ghost avatar Sep 29 '14 04:09 ghost

Here's how I solved this one:

My hidden columns have a .hidden class to each <td> in the column and I omit a row by adding a .omit-row class to the <tr>

Then, I take a clone of the html DOM element and delete the cells and rows out of the clone before I send it over to jspdf

<div id="table">
<table>
...
</table>
</div>

(I realize that I totally abuse jquery's $.each() below... )

    source = $('#table')[0];
    cln_source = source.cloneNode(true);
    //console.log(cln_source);
    var deleterows = [];
    $(cln_source.childNodes[1].rows).each(function(ri, row){
        var deletecells = [];
        $(row.cells).each(function(ci, cell){
            if( $(cell).hasClass('hidden') )
                deletecells.push(ci);
        });
        console.log(deletecells);
        $.each(deletecells.reverse(), function(i, ii){
            console.log(ii); 
            row.deleteCell(ii);
        });
        if($(row).hasClass('omitted-row'))
                deleterows.push(ri);

    });
    $.each(deleterows.reverse(), function(i, ii){
        cln_source.childNodes[1].deleteRow(ii);
    });

    specialElementHandlers = {
        '#bypassme': function (element, renderer) {
            return true
        }
    };
    margins = {
        top: 100,
        bottom: 60,
        left: 40
    };
    pdf.fromHTML(
    cln_source,
    margins.left, 
    margins.top, { 
        'width': margins.width, 
        'elementHandlers': specialElementHandlers
    },

    function (dispose) {
        pdf.save('Test.pdf');
    }, margins);

neildaemond avatar Dec 02 '14 20:12 neildaemond

Wow. Thanks dude! I appreciate your answer.

Sent from Samsung Mobile

-------- Original message --------
From: Neil Pahl
Date:12/03/2014 4:14 AM (GMT+08:00)
To: MrRio/jsPDF
Cc: riichii
Subject: Re: [jsPDF] html table with hidden column (#366)
Here's how I solved this one: My hidden columns have a .hidden class to each in the column and I omit a row by adding a .omit-row class to the Then, I take a clone of the html DOM element and delete the cells and rows out of the clone before I send it over to jspdf
...

(I realize that I totally abuse jquery's $.each() below... )

source = $('#table')[0];
cln_source = source.cloneNode(true);
//console.log(cln_source);
var deleterows = [];
$(cln_source.childNodes[1].rows).each(function(ri, row){
    var deletecells = [];
    $(row.cells).each(function(ci, cell){
        if( $(cell).hasClass('hidden') )
            deletecells.push(ci);
    });
    console.log(deletecells);
    $.each(deletecells.reverse(), function(i, ii){
        console.log(ii); 
        row.deleteCell(ii);
    });
    if($(row).hasClass('omitted-row'))
            deleterows.push(ri);

});
$.each(deleterows.reverse(), function(i, ii){
    cln_source.childNodes[1].deleteRow(ii);
});

specialElementHandlers = {
    '#bypassme': function (element, renderer) {
        return true
    }
};
margins = {
    top: 100,
    bottom: 60,
    left: 40
};
pdf.fromHTML(
cln_source,
margins.left, 
margins.top, { 
    'width': margins.width, 
    'elementHandlers': specialElementHandlers
},

function (dispose) {
    pdf.save('Test.pdf');
}, margins);

— Reply to this email directly or view it on GitHub.

ghost avatar Dec 03 '14 02:12 ghost

@neildaemond or @riichii

Do you guys have a working example for this issue? I am also trying to export tables with hidden columns and still no solution. Please help

ggxt avatar Aug 13 '15 13:08 ggxt

Up

Jeje2201 avatar Jun 23 '18 10:06 Jeje2201

We are closing this issue, because we will not support any longer fromHTML and addHTML.

Explaination: We are working on a new html2pdf plugin, which will be based on html2canvas and our context2d plugin. This should lead to more reliable results for your projects. And it will give us the time to focus on the core functionality of pdf-generation because we will not use our energy for writing/supporting/extending 2 html plugins. If you still want to use addHTML or fromHTML you can still use jsPDF 1.4.1.

Best Regards

Uzlopak avatar Sep 06 '18 20:09 Uzlopak