首页 > 解决方案 > jspdf脚本的问题

问题描述

我在我的网站上添加了这个 jspdf 脚本来下载 pdf。但是我得到这个错误。

        Uncaught TypeError: Cannot read property '1' of undefined
            at f.renderParagraph (jspdf.min.js:202)
            at f.setBlockBoundary (jspdf.min.js:202)
            at k (jspdf.min.js:202)
            at k (jspdf.min.js:202)
            at k (jspdf.min.js:202)
            at jspdf.min.js:202
            at l (jspdf.min.js:202)
            at Image.i.onerror.i.onload (jspdf.min.js:202)

这发生在某些页面上,而其他页面工作正常。我在下面添加了我正在使用的代码。我不确定这是否与我的代码或 jspdf 有关。

    //Code I am using: 
        <script type="text/javascript">
        function HTMLtoPDF(){
        var pdf = new jsPDF('p', 'pt', 'letter');
        source = $('#HTMLtoPDF')[0];
        specialElementHandlers = {
            '#bypassme': function(element, renderer){
                return true
            }
        }
        margins = {
                top: 50,
                left: 60,
                right:60,
                width: 545
            };
        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('Download.pdf');
                    }
            )
        }

        </script>
        <button type="button" onclick="HTMLtoPDF()" style=" height: 40px; width: 154px; background-color: #008800; color: #ffffff; font-size: 150%;">Download PDF </button>

        <script src = "https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"> </script>
        <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

标签: jspdf

解决方案


script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
  <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>    
<script type="text/javascript">
$(document).ready(function() {
var str_pdf = $("#HTMLtoPDF").html();
var regex = /<br\s*[\/]?>/gi;
$("#HTMLtoPDF").html(str_pdf.replace(regex, '<p data-empty="true"><br></p>'));
});
function HTMLtoPDF(){
    var pdf = new jsPDF('p', 'pt', 'a4');
    source = $('#HTMLtoPDF')[0];
    specialElementHandlers = {
        '#bypassme': function (element, renderer) {
            return true
        }
    };
    margins = {
        top: 30,
        bottom: 30,
        left: 40,
        width: 522
    };
    pdf.fromHTML(
    source, 
    margins.left, 
    margins.top, { 
        'width': margins.width, 
        'elementHandlers': specialElementHandlers
    },

    function (dispose) {
        pdf.save('Download.pdf');
    }, margins);
}
</script>

<!-- these js files are used for making PDF -->

推荐阅读