14

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');
                }
          });
       }
AddyProg
  • 2,960
  • 13
  • 59
  • 110
  • 6
    lol your question was my answer. Doing what you did there correctly added the image for me – owen gerig Feb 01 '16 at 17:35
  • @owengerig: Glad to know my post helped you :) – AddyProg Feb 02 '16 at 07: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 Aug 11 '17 at 13:48

2 Answers2

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/

diegocr
  • 1,000
  • 8
  • 13
0

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

DyC
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 09 '22 at 00:15