简体   繁体   English

下载HTML <div> 使用jsPDF转换为pdf

[英]download html <div> into pdf using jsPDF

I am trying to download a div value into PDF using jsPDF here are the code which I have written: 我正在尝试使用jsPDF将div值下载到PDF中,这里是我编写的代码:

<html>
  <head>
    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script src="jsPDF.js"></script>
    <script>
    $(function () {
      var doc = new jsPDF();
      var specialElementHandlers = {
        '#editor': function (element, renderer) {
          return true;
        }   
      };

      $('#cmd').click(function () {
        doc.fromHTML($('#content').html(), 15, 15, {
          'width': 170,
          'elementHandlers': specialElementHandlers
        });
        doc.save('sample-file.pdf');
      });
    });
    </script>
  </head>
  <body>
    <div id="content">
      <h3>Hello, this is a H3 tag</h3>
      <p>a pararaph</p>
    </div>
    <div id="editor"></div>
    <button id="cmd">generate PDF</button>
  </body>
</html>

But while clicking on the button nothing is happening...I have downloaded the jsPDF from this link . 但是点击按钮时没有发生任何事情......我已经从这个链接下载了jsPDF。

I am clueless of thing can anyone please help me. 我无能为力,任何人都可以帮助我。

Pick jspdf.min.js from https://github.com/MrRio/jsPDF/tree/master/dist https://github.com/MrRio/jsPDF/tree/master/dist中选择jspdf.min.js

Then this should work: 那应该工作:

....
<script src="jspdf.min.js"></script>
<script>
$(function () {

  $('#cmd').click(function () {
    var doc = new jsPDF();
    doc.addHTML($('#content')[0], 15, 15, {
      'background': '#fff',
    }, function() {
      doc.save('sample-file.pdf');
    });
  });
});
</script>
....
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.js"></script>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.js"></script>
      <script type="text/javascript" src="http://mrrio.github.io/jsPDF/dist/jspdf.debug.js"></script>
      <script type="text/javascript" src="http://html2canvas.hertzen.com/build/html2canvas.js"></script>
  <script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(document).ready(function() {
    $('#download').click(function() {       
        html2canvas($("#canvas"), {
            onrendered: function(canvas) {         
                var imgData = canvas.toDataURL('image/png'); 
                $("#imgRes").attr("src", imgData);             
                var doc = new jsPDF('p', 'mm');
                doc.addImage(imgData, 'PNG', 10, 10);
                doc.save('sample-file.pdf');
            }
        });
    });
});
});//]]> 
</script>

and here is a HTML its working i tested it many times. 这是一个HTML工作,我测试了很多次。 and using this same code. 并使用相同的代码。

<img id="imgRes" height="500px" width="770px"  />
   <div id="canvas" style="background-image:url(fimage/1.jpg)"><br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</div> 
<button id="download">Download Pdf</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM