Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsPDF outputs blank document

I am using jsPDF to save the output of a div on my page to PDF

My Code so far is:

function makepdf() {

var doc = new jsPDF();
var html=jQuery('.js-requester-info').html();
alert (html);
doc.fromHTML(html , 15, 15, {
    'width': 800
});

     doc.save('test');
}

The code works, but the generated PDF file is always blank. I have added a "debug" line and

alert (html);

outputs some html code from the div, but how come the PDF is always empty?

UPDATE: I added some delay ( I found someone talnkig about delay in rendering) and now it's working:

setTimeout(function(){
doc.save('test');
},2000);
like image 668
Antonio Avatar asked Mar 01 '26 06:03

Antonio


1 Answers

You can add a callback option

let doc = new JsPDF({ orientation: 'p', format: 'a4' })
console.log(document.getElementById('offer').innerHTML)
doc.fromHTML(document.getElementById('offer').innerHTML, 1, 1, {
  elementHandlers: function() {
    return true
  }
}, function() {
  doc.save('test')
})
like image 152
Haluk Keskin Avatar answered Mar 03 '26 18:03

Haluk Keskin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!