Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将表格从jsp页面导出为pdf_Java_Jsp_Pdf - Fatal编程技术网

Java 如何将表格从jsp页面导出为pdf

Java 如何将表格从jsp页面导出为pdf,java,jsp,pdf,Java,Jsp,Pdf,我正在尝试将一个表从jsp页面导出到pdf。jspdf也不适用于此。 我的jsp页面如下所示,当我单击按钮export to pdf时,什么都没有发生 <!DOCTYPE html> <% HeadCountReport hcOb = new HeadCountReport(); String groupName = request.getAttribute("gn").toString(); %> <%@page import="com.mypay.Applic

我正在尝试将一个表从jsp页面导出到pdf。jspdf也不适用于此。 我的jsp页面如下所示,当我单击按钮export to 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>
您好,我正在尝试将一个表从jsp页面导出到pdf。jspdf也不适用于此。 我的jsp页面如下所示,当我单击按钮export to 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>

分组人数
函数loadTable(){
document.getElementById('tbldiv')。宽度=screen.availWidth;
document.getElementById('tbldiv')。高度=screen.availHeight;
}
var tableToExcel=(函数(){
var uri='data:application/vnd.ms excel;base64,'
,模板=“{table}”
,base64=函数{return window.btoa(unescape(encodeURIComponent))}
,format=函数(s,c){返回s.replace(/{(\w+)}/g,函数(m,p){返回c[p];})}
返回函数(表、名称){
如果(!table.nodeType)table=document.getElementById(table)
var ctx={工作表:名称| |'工作表',表:table.innerHTML}
window.location.href=uri+base64(格式(模板,ctx))
}
})()
函数printPDF(){
var printDoc=new jsPDF();
printDoc.fromHTML($('tblGrp').get(0),10,10,{'width':180});
printDoc.autoPrint();
printDoc.output(“dataurlnewwindow”);//这将打开一个新的弹出窗口,之后PDF将打开“打印窗口”视图,但浏览器处理方式与此不一致
}
函数open\u win(url\u add)
{
打开(url_add,'welcome','width=300,height=200,menubar=no,status=no,location=no,toolbar=no,scrollbars=no');
}
函数myNewWindow(url\u添加){
打开(url\u添加“新建窗口”,“高度=200,宽度=200,状态=yes,菜单栏=no,状态=no,位置=no,工具栏=no,滚动条=no”)
}
小组休息HC报告


斯尔诺 团体 分部 员工类型 人数

谢谢。

我看到javascript函数调用id=“tblGrp”。但是,

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

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

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

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

3) 这里是iText替代方案的附加帖子。 [


如果没有关于javascript错误的更多信息,这里就没有什么可解决的了。

您有没有看一下这个:?经过多次尝试,我最终使用Jasper reports创建了独立于平台的PDF创建。Jasper+iReport是IMHO的方法。有关详细信息,请访问