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'); } }); } 
3
  • 6
    lol your question was my answer. Doing what you did there correctly added the image for me Commented Feb 1, 2016 at 17:35
  • @owengerig: Glad to know my post helped you :) 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'); } }); Commented Aug 11, 2017 at 13:48

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

4 Comments

it says ..pdf.addHTML(document.body,function()
sorry, means debugger is not recognizing this addHTML function
addHTML is a new jsPDF plugin, pick the latest version from github.com/MrRio/jsPDF/tree/master/dist
blurry image found using this sadly
0

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

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.