HTML5 canvas, convert canvas to PDF with jspdf.js

前端 未结 5 666
陌清茗
陌清茗 2021-02-04 12:10

I am trying to convert HTML5 canvas to PDF in JavaScript but I get a black background PDF. I tried to change the background color but still get black. The following is code I am

5条回答
  •  野性不改
    2021-02-04 12:58

    A good approach is to use combination of jspdf.js and html2canvas. I have made a jsfiddle for you.

      
          
    

    '

            html2canvas($("#canvas"), {
                onrendered: function(canvas) {         
                    var imgData = canvas.toDataURL(
                        'image/png');              
                    var doc = new jsPDF('p', 'mm');
                    doc.addImage(imgData, 'PNG', 10, 10);
                    doc.save('sample-file.pdf');
                }
            });
    

    jsfiddle: http://jsfiddle.net/rpaul/p4s5k59s/5/

提交回复
热议问题