首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何正确使用jsPDF库

如何正确使用jsPDF库
EN

Stack Overflow用户
提问于 2013-05-31 21:53:25
回答 5查看 390.5K关注 0票数 93

我想要将我的一些div转换成PDF,我已经尝试了jsPDF库,但没有成功。看起来我不能理解我需要导入什么才能使这个库工作。我已经看过这些例子了,但我还是搞不懂。我尝试过以下几种方法:

代码语言:javascript
运行
复制
<script type="text/javascript" src="js/jspdf.min.js"></script>

在jQuery和之后:

代码语言:javascript
运行
复制
$("#html2pdf").on('click', function(){
    var doc = new jsPDF();
    doc.fromHTML($('body').get(0), 15, 15, {
        'width': 170
    });
    console.log(doc);
});

用于测试目的,但我收到:

代码语言:javascript
运行
复制
"Cannot read property '#smdadminbar' of undefined"

其中#smdadminbar是主体的第一个div。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-10-10 16:39:22

您可以使用html中的pdf,如下所示:

步骤1:将以下脚本添加到头部

代码语言:javascript
运行
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>

or download locally

第2步:添加执行jsPDF代码的HTML脚本

自定义此参数以传递标识符,或仅将#content更改为所需的标识符。

代码语言:javascript
运行
复制
 <script>
    function demoFromHTML() {
        var pdf = new jsPDF('p', 'pt', 'letter');
        // source can be HTML-formatted string, or a reference
        // to an actual DOM element from which the text will be scraped.
        source = $('#content')[0];

        // we support special element handlers. Register them with jQuery-style 
        // ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
        // There is no support for any other type of selectors 
        // (class, of compound) at this time.
        specialElementHandlers = {
            // element with id of "bypass" - jQuery style selector
            '#bypassme': function (element, renderer) {
                // true = "handled elsewhere, bypass text extraction"
                return true
            }
        };
        margins = {
            top: 80,
            bottom: 60,
            left: 40,
            width: 522
        };
        // all coords and widths are in jsPDF instance's declared units
        // 'inches' in this case
        pdf.fromHTML(
            source, // HTML string or DOM elem ref.
            margins.left, // x coord
            margins.top, { // y coord
                'width': margins.width, // max width of content on PDF
                'elementHandlers': specialElementHandlers
            },

            function (dispose) {
                // dispose: object with X, Y of the last line add to the PDF 
                //          this allow the insertion of new lines after html
                pdf.save('Test.pdf');
            }, margins
        );
    }
</script>

第三步:添加你的正文内容

代码语言:javascript
运行
复制
<a href="javascript:demoFromHTML()" class="button">Run Code</a>
<div id="content">
    <h1>  
        We support special element handlers. Register them with jQuery-style.
    </h1>
</div>

Refer to the original tutorial

See a working fiddle

票数 144
EN

Stack Overflow用户

发布于 2014-11-07 00:23:46

您只需要此链接jspdf.min.js

里面什么都有。

代码语言:javascript
运行
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
票数 18
EN

Stack Overflow用户

发布于 2013-06-28 06:10:12

您不是也应该使用jspdf.plugin.from_html.js库吗?除了主库(jspdf.js)之外,您还必须使用其他库来执行“特殊操作”(比如使用图像的jspdf.plugin.addimage.js )。检查https://github.com/MrRio/jsPDF

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16858954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档