如何使用jspdf在同一文件中添加不同的div

时间:2017-07-13 05:52:30

标签: html jspdf

我想转换另一个id为id =" page2"在我的HTML图像中,并将其添加到同一pdf文件中的另一页上 这是我的代码:

   <script type="text/javascript" src="jspdf.min.js"></script>
   <script type="text/javascript" src="html2canvas.js"></script>
   <script type="text/javascript">
     function genPDF() {
       html2canvas(document.getElementById('hoja1'),{
         onrendered: function (canvas) {
         var img = canvas.toDataURL("image/png");
         var doc= new jsPDF();
         doc.addImage(img,'JPEG',0,0,210,230);
         doc.save('ContratoMutuo.pdf');
        }
       });
     }
   </script>

但只能使用一个div

和html代码是:

        <!DOCTYPE html>
        <html>
          <head>
            <meta charset="utf-8">
            <title>file</title>
            <script type="text/javascript" src="jspdf.min.js"></script>
            <script type="text/javascript" src="html2canvas.js"></script>
            <script type="text/javascript">
              function genPDF() {
                html2canvas(document.getElementById('hoja1'),{
                  onrendered: function (canvas) {
                  var img = canvas.toDataURL("image/png");
                  var doc= new jsPDF();
                  doc.addImage(img,'JPEG',0,0,50,50);
                  doc.save('test.pdf');
                 }
                });
              }
            </script>
          </head>
          <body>
            <div id="hoja1">
                <TABLE WIDTH=300 HEIGHT=200>
                  <TD width=100 BGCOLOR="red">prueba </TD>
                  <TD WIDTH=100 BGCOLOR="yellow">prueba </TD>
                  <TD WIDTH=100 BGCOLOR="gray">prueba </TD>
                </TABLE>
            </div>
            <div id="hoja2">
              <TABLE WIDTH=300 HEIGHT=200>
                <TD width=100 BGCOLOR="green">prueba </TD>
                <TD WIDTH=100 BGCOLOR="blue">prueba </TD>
                <TD WIDTH=100 BGCOLOR="gray">prueba </TD>
              </TABLE>
            </div>
            <a href="javascript: genPDF()">DOWNLOAD PDF</a>
          </body>
        </html>

完整的文件应该是:

1/2

2/2

在同一个文件中请帮忙!!三江源!

1 个答案:

答案 0 :(得分:0)

如果我理解了你想要存档的内容,这可能会有效:

    function genPDF() {
                           html2canvas(document.getElementById('hoja1'),{
                             onrendered: function (canvas) {
                               html2canvas(document.getElementById('hoja2'),{
                                 onrendered: function (canvas2) {
                                 var img = canvas.toDataURL("image/png");
                                 var doc= new jsPDF();
                                 doc.addImage(img,'JPEG',0,0,210,230);
                                 var img2 = canvas2.toDataURL("image/png");
                                 doc.addPage();
                                 doc.addImage(img2,'JPEG',0,0,210,230);
                                 doc.save('ContratoMutuo.pdf');
                            }
                           });
                         }
                         });
                       }