Humberto Gutierrez
Humberto Gutierrez

Reputation: 21

jspdf export html (div) to pdf not working

Im triying to generate a PDF only with a div content (can have images)

im trying HTML -> PDF

this can help how to use addHTML function in jsPDF and jsPdf using html2canvas.js for export pdf from html is not working for large data

This is my code:

$("#pdfButton").click(function(){
  $(".compare_wrapper").clone().appendTo("#printDiv");
  var pdf = new jsPDF('l', 'pt', 'a4');
  var options = {
    pagesplit: true
  };
  pdf.addHTML($('#printDiv'), 0, 0, options, function(){
    pdf.save("test.pdf");
  });
  $("#printDiv").empty();
});
});
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.debug.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>

<div id="printDiv" style='padding:0;top:0;left:0;margin:0;' hidden></div>

<div class="row">  
        <div class="compare_wrapper col-md-12"> <!--Here i show the data-->

it give me this error: jspdf.debug.js:4455 Uncaught Error: Supplied data is not a JPEG

i try

var pdf = new jsPDF('l', 'pt', 'a4');
        var options = {
            pagesplit: true
        };
        pdf.addHTML($('body'), 0, 0, options, function(){
            pdf.save("test.pdf");
        });

And works but show me all the body, i only need the content of the div id="printDiv" i use the lib html2canvas

Upvotes: 0

Views: 3024

Answers (1)

GBishton
GBishton

Reputation: 11

you should be able to use a JQuery selector to choose just the div that you want in the pdf, like this:

var pdf = new jsPDF('l', 'pt', 'a4');
        var options = {
            pagesplit: true
        };
        pdf.addHTML($('#printDiv'), 0, 0, options, function(){
            pdf.save("test.pdf");
        });

Upvotes: 1

Related Questions