简体   繁体   中英

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);

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')
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM