Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Canvas to Pdf using JsPDF

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');
                }
          });
       }
like image 729
Addy Avatar asked Aug 11 '14 06:08

Addy


People also ask

How do I create a multipage PDF in HTML?

Step 1) Include jsPDF and html2canvas liberary URl in head section of your HTML. In this code block we have used html2canvas function which will give canvas of HTML section we specified, then after getting canvas object we will add PDF page using a jsPDF method and add break-up of canvas s image(JPG) in PDF page.

What is JavaScript jsPDF?

jsPDF is an open-source library for generating PDF documents using JavaScript. It provides multiple options to generate PDF files, with custom properties. It has numerous plugins to support various ways of PDF generation.


1 Answers

Try this instead:

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

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

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

like image 121
diegocr Avatar answered Oct 06 '22 00:10

diegocr