TypeError: pdf.fromHTML не является функцией при преобразовании html в pdf с использованием jspdf

Я пытаюсь преобразовать код jsp в файл PDF с помощью jsPDF. Но это дает мне TypeError: pdf.fromHTML не является функцией. Я скачал zip-архив jspdf с сайта https://github.com/MrRio/jsPDF. Мой код:

 <html>
<head>

<title>Exporting table data to pdf Example</title>


<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.js"></script>
<script src="jsPDF/jspdf.js"></script>
<script src="jsPDF/main.js"></script>



<script type="text/javascript">
    $(document).ready(function() {

        $("#exportpdf").click(function() {
            var pdf = new jsPDF('p', 'pt', 'ledger');
            // source can be HTML-formatted string, or a reference
            // to an actual DOM element from which the text will be scraped.
            source = $('#yourTableIdName')[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 : 60,
                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('fileNameOfGeneretedPdf.pdf');
            }, margins);
        });

    });
</script>



</head>
<body>
<div id="yourTableIdName">
    <table style="width: 1020px;font-size: 12px;" border="1">
        <thead>
            <tr align="left">
                <th>Country</th>
                <th>State</th>
                <th>City</th>
            </tr>
        </thead>


        <tbody>

            <tr align="left">
                <td>India</td>
                <td>Telangana</td>
                <td>Nirmal</td>
            </tr>
<tr align="left">
                <td>India</td>
                <td>Telangana</td>
                <td>Nirmal</td>
            </tr><tr align="left">
                <td>India</td>
                <td>Telangana</td>
                <td>Nirmal</td>
            </tr><tr align="left">
                <td>India</td>
                <td>Telangana</td>
                <td>Nirmal</td>
            </tr><tr align="left">
                <td>India</td>
                <td>Telangana</td>
                <td>Nirmal</td>
            </tr><tr align="left">
                <td>India</td>
                <td>Telangana</td>
                <td>Nirmal</td>
            </tr><tr align="left">
                <td>India</td>
                <td>Telangana</td>
                <td>Nirmal</td>
            </tr>
        </tbody>
    </table></div>

    <input type="button" id="exportpdf" value="Download PDF">


</body>

</html>

person Pratty    schedule 26.05.2016    source источник


Ответы (2)


Вы загружаете не тот файл.

В библиотеках Javascript есть необработанные исходные файлы, а есть скомпилированные файлы, готовые к распространению. Поэтому всякий раз, когда вы просматриваете такую ​​библиотеку и хотите найти файл для включения, обычно проверяется каталог dist/. Итак, все, что вам нужно включить, это либо jspdf.debug.js, либо jspdf.min.js.

Примеры, включенные в репозиторий, — хороший способ узнать, как включать и использовать библиотеку. Ознакомьтесь с основным примером здесь:

https://github.com/MrRio/jsPDF/blob/master/examples/basic.html

person Omar Shehata    schedule 26.05.2016

Просто используйте следующие зависимости.

<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.debug.js"></script>

Убедитесь, что вы не включили какой-либо другой файл jspdf.js, который переопределит файл jspdf.debug.js. Включение нескольких версий также приведет к проблемам.

person Chirram Kumar    schedule 11.01.2017