繁体   English   中英

使用html2canvas和jsPDF将主体转换为pdf

[英]Convert body to pdf with html2canvas and jsPDF

大家早上好,我发现可以转换为pdf的脚本

当信息进入标签form时,他的作品很好。

 (function(){ var form = $('.form'), cache_width = form.width(), a4 =[ 595.28, 841.89]; // for a4 size paper width and height $('#create_pdf').on('click',function(){ $('body').scrollTop(0); createPDF(); }); //create pdf function createPDF(){ getCanvas().then(function(canvas){ var img = canvas.toDataURL("image/png"), doc = new jsPDF({ unit:'px', format:'a4' }); doc.addImage(img, 'JPEG', 20, 20); doc.save('techumber-html-to-pdf.pdf'); form.width(cache_width); }); } // create canvas object function getCanvas(){ form.width((a4[0]*1.33333) -80).css('max-width','none'); return html2canvas(form,{ imageTimeout:2000, removeContainer:true }); } }()); 
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>HTML to PDF - techumber</title> <link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/semantic-ui/1.12.0/semantic.min.css"> </head> <body> <div class="ui page grid"> <div class="wide column"> <h1 class="ui header aligned center">HTML to PDF</h1> <div class="ui divider hidden"></div> <div class="ui segment"> <div class="ui button aligned center teal" id="create_pdf">Create PDF</div> <div class="ui divider"></div> <form class="ui form"> <p class="ui paragraph">oasdojasodjasjdaisjdoasjdoij</p> </form> </div> </div> </div> <!-- scripts --> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript" src="http://cdn.rawgit.com/niklasvh/html2canvas/0.5.0-alpha2/dist/html2canvas.min.js"></script> <script type="text/javascript" src="http://cdn.rawgit.com/MrRio/jsPDF/master/dist/jspdf.min.js"></script> <script type="text/javascript" src="app.js"></script> </body> </html> 

如何将app.js中的var代码转移到此

在链接到文章?tmpl=component&print=1后,我的脚本生成了任何打印格式的文章?tmpl=component&print=1问题是如何将?tmpl=component&print=1var而不是标签form ,可以尝试吗?

$(document).ready(function(){ 
        $("a").click(function(){
          var link = $(this).attr("href");
        });

      });

或类似的东西?

UPD! 在回答@Sergion Alen之后,有了这个主意:

$(document).ready(function(){ 
        $("a").click(function(){
          var link = window.location.href + '?tmpl=component&print=1';
          alert(link);
        });

      });

显示我所需要的...如何在此处编写数组link显示(替换数组link.forms.forms

form = $('.form')

在app.js中

然后,您在正确的轨道上,请尝试以下操作:

$(document).ready(function(){ 
  $("a").on('click', function(e){
    e.preventDefault();
    var link = $(this).attr("href");
    document.location = link + '?tmpl=component&print=1';
  });
});

暂无
暂无

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

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