0

I am trying to export a table from jsp page to pdf. jspdf is also not working for this. my jsp page goes as below, nothing is happening when I click the button export to pdf

Hi, I am trying to export a table from jsp page to pdf. jspdf is also not working for this. my jsp page goes as below, nothing is happening when I click the button 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>

thanks.

  • Did you took a look at this : http://stackoverflow.com/questions/13717743/how-to-export-table-displayed-on-jsp-to-pdf-in-java-strust2 ? – VERYNET Oct 07 '15 at 12:08
  • After many tries I ended up using Jasper reports to create platform independent PDF creation. Jasper + iReport is the way to go IMHO. Details for the same can be found at http://community.jaspersoft.com/ – Jabir Oct 07 '15 at 12:20

1 Answers1

0

I see the javascript function calls id="tblGrp". However,

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

The id appears multiple times, perhaps giving a problem with jspdf. Without more information of the javascript error, Here are a couple of java alternatives.

1) You can build everything using iText.

2) You can design a report in iReport by constructing a jrxml and use the jasper engine for independent pdf exporting/rendering in your application.

3)Here is additional post for alternative to iText. [Are there any Java PDF creation alternatives to iText?

Without more info of the javascript error, there isn't much to solve here.

Community
  • 1
  • 1