使用 JsPDF 将 Canvas 转换为 Pdf

2022-01-17 00:00:00 pdf javascript html5-canvas jspdf

我正在尝试将画布转换为 pdf,但结果是干净的白色 pdf这是代码,我无法弄清楚我错过了什么..

I am trying to convert canvas into pdf but i get clean white pdf in result Here is the code, I am not being able to figure out what i am missing..

function HtmlToImage(){
    html2canvas(document.body, {
    onrendered: function(canvas) {
    var img =canvas.toDataURL("image/jpeg,1.0");  
    var pdf = new jsPDF();
    pdf.addImage(img, 'JPEG', 0, 0);
    pdf.output('datauri');
                }
          });
       }

推荐答案

试试这个:

var pdf = new jsPDF('p','pt','a4');

pdf.addHTML(document.body,function() {
    pdf.output('datauri');
});

参见http://mrrio.github.io/jsPDF/

相关文章