繁体   English   中英

将html表导出为pdf

[英]Exporting html table to pdf

我的代码有什么问题? 我想将html表导出为pdf并保存。 然后,它碰巧只打开了一个空白屏幕的新浏览器。 这是我的示例代码。 请帮忙。谢谢。

<!DOCTYPE html>
<html>
</head>
<body>
<div id="pdf">
<table id="StudentInfoListTable">
            <thead>
                <tr>    
                    <th>Name</th>
                    <th>Email</th>
                    <th>Track</th>
                    <th>S.S.C Roll</th>
                    <th>S.S.C Division</th>
                    <th>H.S.C Roll</th>
                    <th>H.S.C Division</th>
                    <th>District</th>

                </tr>
            </thead>
            <tbody>

                    <tr>
                        <td>alimon  </td>
                        <td>Email</td>
                        <td>1</td>
                        <td>2222</td>
                        <td>as</td>
                        <td>3333</td>
                        <td>dd</td>
                        <td>33</td>
                    </tr>               
            </tbody>
        </table>
    </div>

  <!--<button >Submit</button> -->
  <li> <a href="javascript:PrintDiv()">Export to pdf</a> </li>

<script type="text/javascript">     
    function PrintDiv(htmlexportPDF) {    
       var divToPrint = document.getElementById(htmlexportPDF);
       alert('Printing: Success');
       var popupWin = window.open('', '_blank', 'width=650,height=600');
       popupWin.document.open();
       popupWin.document.write('<style type="text/css">body{ margin:0.5px;');
       popupWin.document.write('font-family:verdana,Arial;color:#000;');
       popupWin.document.write('font-family:Verdana, Geneva, sans-serif; font-size:8px;}');
       popupWin.document.write('a{color:#000;text-decoration:none;}</style>');
       popupWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
       popupWin.document.close();
            }
 </script>

您的JavaScript函数printDiv接受参数htmlexportPDF。

函数PrintDiv(htmlexportPDF){

问题是,当您从<a>链接调用PrintDiv时,您没有提供任何参数。

要解决此问题,请更改

<a href="javascript:PrintDiv()">Export to pdf</a>

<a href="javascript:PrintDiv('StudentInfoListTable')">Export to pdf</a>

另外,在您的HTML代码中,您永远不会放置一个<head>标记或一个</body>标记或一个</html>标记。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM