I have a content div with the id as "content". In the content div I have some graphs and some tables. I want to download that div as a pdf when user click on download button. Is there a way to do that using javascript or jQuery?

推荐答案

You can do it using jsPDF

HTML:

<div id="content">
     <h3>Hello, this is a H3 tag</h3>

    <p>A paragraph</p>
</div>
<div id="editor"></div>
<button id="cmd">generate PDF</button>

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');
});

Jquery相关问答推荐

如何在外部JS文件中使用带有参数的laravel路由

获取 html 元素的 onclick 事件的 data-* 属性

使用 jQuery 时何时/为什么要在变量前加上$?

jQuery在点击函数后获取
  • 元素的id/value
  • 在 JSON 对象上使用 jQuery 的 find()

    如何 Select 具有特定文本的所有锚标记

    jquery清除输入默认值

    jQuery切换文本?

    如何使用 jquery 更改 onclick 事件?

    在完成前一个请求之前中止新的 AJAX 请求

    即时替换 css 文件(并将新样式应用于页面)

    jQuery ajax (jsonp) 忽略超时并且不触发错误事件

    JQuery:如果 div 可见

    虽然未定义变量 - 等待

    $(document).ready(function(){});页面底部的 vs 脚本

    jQuery - 确定输入元素是文本框还是 Select 列表

    Select jQuery UI 自动完成后清除表单字段

    我可以通过 JavaScript 禁用 CSS :hover 效果吗?

    jQuery .val 更改不会更改输入值

    Drop 事件未在 chrome 中触发