This is my data display in table format. I want to show as it is in PDF without using display tag library of struts2.
<table border="1" align="center" style="border-color: #CCCCCC; border-width: 1px; border-style: None; width: 1320px; border-collapse: collapse;" id="tablepaging">
<tbody>
<tr>
<td>Leave ID</td>
<td>FROM DATE</td>
<td>TO DATE</td>
<td>DAYS REQUESTED</td>
<td>APPROVER</td>
<td>NOTES</td>
<td>REMARK</td>
<td>IS PLANNED</td>
<td>REASON</td>
</tr>
<tr>
<td>270</td>
<td>12/27/12</td>
<td>12/29/12</td>
<td>2</td>
<td>Sagar</td>
<td>s</td>
<td>s</td>
<td>true</td>
<td>s</td>
<td>
<a href="/HRIS_Updated/cancelRequest.action;jsessionid=A2313340A50DD2DAB054714BF65AB08B?leaveId=270" id="submitinvoice;jsessionid=A2313340A50DD2DAB054714BF65AB08B_">Cancel</a>
</td>
<td>
<a href="/HRIS_Updated/requestHistory.action;jsessionid=A2313340A50DD2DAB054714BF65AB08B?leaveId=270" id="submitinvoice;jsessionid=A2313340A50DD2DAB054714BF65AB08B_">History</a>
</td>
</tr>
</tbody>
</table>
Is it be possible with javascript or jquery?
Please help me with some code I have googled it for a few days but get nothing.
Using display table on jsp would be quite easire to convert it to *pdf along with .csv,.excel and son on,Here is the sample code ;
<display:table id="data" name="${questions}" requestURI="" pagesize="10" export="true" >
<display:column property="label" title="Question" sortable="true"/>
<display:column title="Graph Analysis"> <img src="${imagePath}${reportData.clientName}/${data.label}.png"/></display:column>
<display:setProperty name="export.pdf" value="true" />
</display:table>
As far as i know unfortunately javascript cannot create pdf files by itself. And i haven't use struts yet. But I recommend you Displaytag library which is very easy to use :)
This is what you need particularly (with code) : http://displaytag.sourceforge.net/10/export.html
documentation (from beginning to end) : http://displaytag.sourceforge.net/10/displaytag.pdf
To generate a PDF
from an HTML
source in Java, you can use iText's HTMLWorker
module (now deprecated, the new project is XMLWorker, but this depends on the iText version you're using).
You can emulate the table you have on JSP page in an String variable of an Action, let's say CreatePDFAction
;
Then, from the JSP, call CreatePDFAction
with a submit button (opening the pdf on a new page, if you want).
In Struts.xml, declare CreatePDFAction
result as stream
result type, with the appropriate contentType
(application/pdf
), and the desired contentDisposition
for specifying the filename, and the behavior: download it (attachment
) or open it in browser (inline
).
Inside the CreatePDFAction
action, you receive the String, instantiate a new document and a new HTMLWorker, feed it with the string containing your HTML, then extract the bytes from the resulting PDF and put it in an InputStream exposed through a getter by the action.
Finaly i got the solution here is the code
<script language="javascript" type="text/javascript">
function Retrivetable()
{
var table = document.getElementById("historyTable");
if (table) {
// If outerHTML property available, use it
if (typeof table.outerHTML == 'string') {
$('#settable').val(table.outerHTML)
// Otherwise, emualte it
} else {
var div = document.createElement('div');
div.appendChild(table.cloneNode(true));
$('#settable').val(div.innerHTML);
}
}
}
</script>
<s:submit
onclick="Retrivetable()"
value="Export to Pdf" action="ExportToPdf" method="ExportPDF" align="bottom"/>
In the action class
public String ExportPDF()
{
tablestruct = "<html><head></head><body>"+tablestruct+"</body></html>";
//System.out.println("After concat "+tablestruct);
try{
String filePath = ServletActionContext.getServletContext().getRealPath("/testpdf.pdf");
System.out.println(filePath);
Document document=new Document(PageSize.LETTER);
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(filePath));
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(tablestruct));
document.close();
System.out.println("Done");
File file = new File(filePath);
inputStream = new DataInputStream( new FileInputStream(file));
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I am not sure about struts, I used itextpdf
in JSP.
http://tutorials.jenkov.com/java-itext/getting-started.html
Hopefuly it will help