Nitish Kumar Diwakar Nitish Kumar Diwakar - 3 years ago 201
HTML Question

jsPDF not ignoring div

I am trying to ignore a div while exporting to pdf via jsPDF.
Here is my code:

HTML:

<div id="print-this">
<p>Text to print</p>
<div id="ignore">
<p>Text To ignore</p>
</div>
<p>More text to print</p>
</div>
<button id="cmd">Print</button>


Jquery:

$(function () {

$('#cmd').click(function () {
var doc = new jsPDF();
var elementHandler = {
'#ignore': function (element, renderer) {
return true;
}
};
doc.addHTML($('#print-this')[0], 15, 15, {
'background': '#fff',
}, function() {
doc.save('sample.pdf');
});
});
});


Instead of ignoring a specific id it is exporting everything under id="print-this".

Answer Source

Super dirty but does the trick:

$(function() {
  $('#cmd').click(function() {
    $('#ignore').hide(); //before the addHTML()
    var doc = new jsPDF();
    doc.addHTML($('#print-this')[0], 15, 15, {
      'background': '#fff',
    }, function() {
      doc.save('sample.pdf');
    });
    $('#ignore').show(); //and directly after its finished
  });
});

JsFiddle

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download