最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jsPDF outputs blank document - Stack Overflow

programmeradmin1浏览0评论

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

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 e 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);
Share Improve this question edited Apr 22, 2016 at 11:49 Antonio asked Apr 22, 2016 at 10:53 AntonioAntonio 53510 silver badges27 bronze badges 3
  • Looks like adding some timeout make it work.... – Antonio Commented Apr 22, 2016 at 11:45
  • 1 interesting, this post solved my problem. Any idea why adding timeout was necessary? and why it was not including in doc? – Shadab Faiz Commented May 7, 2018 at 11:24
  • tried adding timeOut as well, but works on the last page only. the previous pages shows blank :( – mars-o Commented Feb 1, 2019 at 15:54
Add a ment  | 

1 Answer 1

Reset to default 1

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')
})
发布评论

评论列表(0)

  1. 暂无评论