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

javascript - Converting Canvas to Pdf using JsPDF - Stack Overflow

programmeradmin2浏览0评论

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

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');
                }
          });
       }
Share Improve this question asked Aug 11, 2014 at 6:39 AddyProgAddyProg 3,05013 gold badges63 silver badges114 bronze badges 3
  • 6 lol your question was my answer. Doing what you did there correctly added the image for me – owen gerig Commented Feb 1, 2016 at 17:35
  • @owengerig: Glad to know my post helped you :) – AddyProg Commented Feb 2, 2016 at 7:45
  • Me too! Here is the refactor of your code: html2canvas(document.getElementById('comprobante'), { onrendered: function(canvas) { var img =canvas.toDataURL("image/jpeg,1.0"); var pdf = new jsPDF(); pdf.addImage(img, 'JPEG', 0, 0); pdf.output('datauri'); pdf.save('autoprint.pdf'); } }); – Ulises Vargas De Sousa Commented Aug 11, 2017 at 13:48
Add a comment  | 

2 Answers 2

Reset to default 10

Try this instead:

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

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

See http://mrrio.github.io/jsPDF/

function canvas2pdf(){ var img =canvas.toDataURL();
var pdf = new jspdf.jsPDF(); pdf.addImage(img, 'JPEG', 0, 0); pdf.save('canvas.pdf'); }

发布评论

评论列表(0)

  1. 暂无评论