将表格从HTML导出到PDF

时间:2019-11-08 07:08:24

标签: javascript php html mysql

我有一张桌子,我想将数据导出为pdf。我正在尝试通过使用jQuery来实现。我已经安装了所有库文件。我的代码有什么问题或我错过了任何库文件

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
        <meta name="description" content="" />
        <meta name="classification" content="" />

        <!-- BOOTSTRAP CORE STYLE  -->
        <link href="assets/css/bootstrap.css" rel="stylesheet" />

        <script>

    $(document).ready(function(e){
        $("#pdf").click(function(e){
            $("#tablepdf").tableExport({
                type:'pdf',
                escape:'false'
            });
        });
    });

    </script>
    </head>

    <body>

<h2>Export Data to pdf with PHP and MySQL</h2>

<button type="submit" id="pdf"  class="btn btn-info">Export to pdf</button>

<table id="tablepdf" class="table table-striped table-bordered">
<tr>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
<th>Designation</th>
<th>Address</th>
</tr>
<tr>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
<th>Designation</th>
<th>Address</th>
</tr>
<tr>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
<th>Designation</th>
<th>Address</th>
</tr>
<tr>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
<th>Designation</th>
<th>Address</th>
</tr>
</table>
</div>
  </body>
    <!-- FOOTER SECTION END-->
    <!-- JAVASCRIPT FILES PLACED AT THE BOTTOM TO REDUCE THE LOADING TIME  -->
    <!-- CORE JQUERY  -->
    <script src="assets/js/jquery-1.10.2.js"></script>
    <script src="assets/js/tableExport.js"></script>
    <script src="assets/js/jquery.base64.js"></script>
    <!-- BOOTSTRAP SCRIPTS  -->
    <script src="assets/js/bootstrap.js"></script>
      <!-- CUSTOM SCRIPTS  -->
    <script src="assets/js/custom.js"></script>
    <!-- For PDF-->
    <script src="assets/js/jspdf/jspdf.js"></script>
    <script src="assets/js/jspdf/libs/sprintf.js"></script>
    <script src="assets/js/jspdf/libs/base64.js"></script><!-- This templates was made by Colorlib (https://colorlib.com) -->
</html>

当我单击按钮时,它没有给出任何反应。为此,所需的库文件已经安装。

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,所以我刚离开了jquery,就使用了它:

https://github.com/spipu/html2pdf

使用非常简单,您可以将整个外观修改为pdf文件。

答案 1 :(得分:0)

似乎脚本导入顺序不正确。

<!-- FOOTER SECTION END-->
    <!-- JAVASCRIPT FILES PLACED AT THE BOTTOM TO REDUCE THE LOADING TIME  -->
    <!-- CORE JQUERY  -->
    <script src="assets/js/jquery-1.10.2.js"></script>
    <script src="assets/js/tableExport.js"></script>
    <script src="assets/js/jquery.base64.js"></script>
    <!-- BOOTSTRAP SCRIPTS  -->
    <script src="assets/js/bootstrap.js"></script>
      <!-- CUSTOM SCRIPTS  -->
    <script src="assets/js/custom.js"></script>
    <!-- For PDF-->
    <script src="assets/js/jspdf/jspdf.js"></script>
    <script src="assets/js/jspdf/libs/sprintf.js"></script>
    <script src="assets/js/jspdf/libs/base64.js"></script><!-- This templates was made by Colorlib (https://colorlib.com) -->

我认为,应该在导入PDF库之后将custom.js放在最后