【问题标题】:how to export a table from jsp page to pdf如何将表格从jsp页面导出为pdf
【发布时间】:2016-01-04 15:44:47
【问题描述】:

我正在尝试将表格从 jsp 页面导出为 pdf。 jspdf 也不适用于此。 我的jsp页面如下,当我点击按钮导出到pdf时什么都没有发生

您好,我正在尝试将表格从 jsp 页面导出为 pdf。 jspdf 也不适用于此。 我的jsp页面如下,当我点击按钮导出到pdf时什么都没有发生

<!DOCTYPE html>

<%
HeadCountReport hcOb = new HeadCountReport();
String groupName = request.getAttribute("gn").toString();
%>
<%@page import="com.mypay.ApplicationConstants"%>
<%@page import="com.reports.HeadCountReport"%><html dir="ltr" lang="en-US">
<%@taglib uri="http://displaytag.sf.net" prefix="display" %>

<head>
<meta charset="ISO-8859-1">
<script src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="./js/tableExport.js"></script>
<script type="text/javascript" src="js/jquery.base64.js"></script>
<script type="text/javascript" src="js/sprintf.js"></script>
<script type="text/javascript" src="js/jspdf.js"></script>
<script type="text/javascript" src="js/base64.js"></script>
<title>Groupwise Head Count</title>

<meta name="viewport"
    content="initial-scale = 1.0, maximum-scale = 1.0, user-scalable = no, width = device-width">
<!--[if lt IE 9]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->

<link rel="stylesheet" href="css/style.css" media="screen">

<!--[if lte IE 7]><link rel="stylesheet" href="style.ie7.css" media="screen" /><![endif]-->

<link rel="stylesheet" href="css/style.responsive.css" media="all">
    <link rel="stylesheet" type="text/css" href="css/reports.css"/>
    <script type="text/javascript" src="js/sortable.js"></script>


<script src="js/jquery.js"></script>

<script src="js/script.js"></script>


<script type="text/javascript">
function loadTable(){
    document.getElementById('tbldiv').width=screen.availWidth;
    document.getElementById('tbldiv').height=screen.availHeight;
}
var tableToExcel = (function() {
    var uri = 'data:application/vnd.ms-excel;base64,'
      , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
      , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
      , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
    return function(table, name) {
      if (!table.nodeType) table = document.getElementById(table)
      var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
      window.location.href = uri + base64(format(template, ctx))
    }
  })()
</script>
<script type="text/javascript">
function printPDF() {
    var printDoc = new jsPDF();
    printDoc.fromHTML($('#tblGrp').get(0), 10, 10, {'width': 180});
    printDoc.autoPrint();
    printDoc.output("dataurlnewwindow"); // this opens a new popup,  after this the PDF opens the print window view but there are browser inconsistencies with how this is handled
}
    </script>
<script >
function open_win(url_add)
{
window.open(url_add,'welcome','width=300,height=200,menubar=no,status=no,location=no,toolbar=no,scrollbars=no');
}
function myNewWindow(url_add) {
    window.open(url_add, "newWindow", "height=200,width=200,status=yes,menubar=no,status=no,location=no,toolbar=no,scrollbars=no")
    }

</script>
</head>

<body>

<div id="art-main">

<div class="art-sheet clearfix"><header
    class="art-header clearfix"> <%@include file="header.jsp"%>
</header> <nav class="art-nav clearfix"> <%@include file="hmenu.jsp"%>
<jsp:include page="auth.jsp"></jsp:include> </nav>

<div class="art-layout-wrapper clearfix">

<div class="art-content-layout">

<div class="art-content-layout-row">

<div class="art-layout-cell art-content clearfix"><article
    class="art-post art-article">
<div class="art-postcontent art-postcontent-0 clearfix">
<div class="art-content-layout">

<div class="art-content-layout-row">

<div class="art-layout-cell layout-item-0" style="width: 100%">


<div class="clearfix" align="center"> <h2 align="left">Group Break HC Report</h2>
<p>&nbsp;</p>
   <input type="button" onclick="tableToExcel('tblGrp', 'Groupwise Break Up')" value="Export to Excel"><input type="button" onClick ="javascript:printPDF();" value="Export to PDF"><br/>
 <div id="tblGrp">
 <table width="70%" id="tblGrp" align="center" class="sortable">
      <tr>
        <th width="35" scope="col">SLNo</th>
        <th width="89" scope="col">Group</th>
       <th width="190" scope="col">Division</th>
        <th width="84" scope="col">Staff Type</th>
        <th width="107" scope="col">Head Count</th>
     </tr>   
      <%
      java.util.List<String[]> listDetail=null;
      listDetail = hcOb.getGroupwiseReport(groupName);
      Iterator<String[]> itemplid = listDetail.iterator();
        int count = 0;
      while(itemplid.hasNext()){
        String[] sepEmpDet = itemplid.next();  
      %>
      <tr>
        <td><%=++count%></td>
        <td><%=sepEmpDet[1] %></td>
        <td><%=sepEmpDet[3] %></td>
        <td><%=sepEmpDet[2] %></td>
        <td><%=sepEmpDet[0] %></td>
      </tr>
<%       }

      %>

    </table>      

    </div>
</div>

</div>

</div>

</div>

</article></div>


</div>

</div>

</div>
<%@include file="footer.jsp"%></div>
</div>
</body>
</html>

谢谢。

【问题讨论】:

标签: java jsp pdf


【解决方案1】:

我看到 javascript 函数调用 id="tblGrp"。不过,

 <div id="tblGrp">
 <table width="70%" id="tblGrp" align="center" class="sortable">

id出现多次,可能是jspdf有问题。如果没有更多关于 javascript 错误的信息,这里有几个 java 替代方案。

1) 您可以使用 iText 构建所有内容。

2) 您可以在 iReport 中通过构建 jrxml 来设计报表,并在您的应用程序中使用 jasper 引擎进行独立的 pdf 导出/渲染。

3)这里是替代 iText 的附加帖子。 [Are there any Java PDF creation alternatives to iText?

没有更多关于 javascript 错误的信息,这里没有太多要解决的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多