如何把HTML中的div的内容下载到使用JS的pdf文件中? [英] How to put and download the content of a div in html into pdf file using JS?

查看:418
本文介绍了如何把HTML中的div的内容下载到使用JS的pdf文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将div的内容放入pdf文件中,然后下载它。我搜索了一下,发现可以通过 jsPDF 使用javascript来完成此操作。



我发现这个示例代码,但它似乎不适合我。当我点击生成PDF按钮时,什么也没有发生。

 < div id =我的网站也使用codeigniter作为我的框架,我的网站目前已上传到000.webhost.com。 内容 > 
< h3>您好,这是一个H3标签< / h3>

< p> a pararaph< / p>
< / div>
< div id =editor>< / div>
< button id =cmd>生成PDF< / button>

< script type =text / javascript>
var doc = new jsPDF();
var specialElementHandlers = {
'#editor':function(element,renderer){
return true;
}
};
$ b $('#cmd')。click(function(){
doc.fromHTML($('#content')。html(),15,15,{
'width':170,
'elementHandlers':specialElementHandlers
});
doc.save('sample-file.pdf');
});

< / script>


解决方案

适用于JSFiddle.net:
http://jsfiddle.net/scottcanoni/h3Lb05wa/



你在网站上发生了什么错误?

所有我包含的是jQuery和jsPDF脚本:

 < script type =text / javascriptsrc =https://parall.ax/parallax/js/jspdf.js>< / script> ; 

如果您在< head>中包含jQuery和jsPDF 标记,你应该罚款提供的代码。


I need to put the content of a div into a pdf file then download it. I've searched a bit and I found that you can use javascript to do this via jsPDF.

I found this sample code but it seems that it doesn't work for me. When I click the generate pdf button nothing happens. I am also using codeigniter for my framework and my website is currently uploaded in 000.webhost.com.

     <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>

         <script type="text/javascript">
       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>

解决方案

Works for me on JSFiddle.net: http://jsfiddle.net/scottcanoni/h3Lb05wa/

What error are you getting on your website?

All I am including is jQuery and the jsPDF script:

<script type="text/javascript" src="https://parall.ax/parallax/js/jspdf.js"></script>

If you include both jQuery and jsPDF in your <head> tag, you should be fine with the code provided.

这篇关于如何把HTML中的div的内容下载到使用JS的pdf文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆