【问题标题】:error in using jspdf to export data使用jspdf导出数据出错
【发布时间】:2017-01-25 07:47:24
【问题描述】:

我正在使用 asp.net 在 angularJS 中创建一个 Web 应用程序, 在这里我想将数据导出到 pdf 文件中,但我无法获取

我在 stackoverflow 上看到了一个工作示例

Click Here

我创建了一个同样不起作用的 jsfiddle

Click here for jsfiddle

这是我的代码

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js" rel="Stylesheet" />
    <link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="Stylesheet" />
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<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>
<script type="text/javascript">
    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>
</asp:Content>

这是我的完整页面,以便更好地理解

问题

1) 为什么会出现错误

2)我需要做什么来解决这个错误

【问题讨论】:

    标签: javascript jquery asp.net angularjs


    【解决方案1】:

    您缺少 jquery 链接,因为您有使用 jquery 调用的源。

    https://code.jquery.com/jquery-latest.min.js
    

    https://jsfiddle.net/noitse/xa9jrwr4/5/ 我已经直接调用函数了

    编辑:这是没有 jquery 的版本 https://jsfiddle.net/noitse/xa9jrwr4/6/

     source = document.getElementById('content')
    

    用javascript调用源

    【讨论】:

    • 但我想在按钮点击时导出它
    • 当你在你的网站上制作它时你就可以了,jsfiddle在调用html函数时表现得很有趣
    猜你喜欢
    • 2021-06-05
    • 1970-01-01
    • 2013-11-17
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 2015-03-30
    • 2021-12-27
    相关资源
    最近更新 更多