How to export table displayed on jsp to pdf in java strust2

This is my table format data display. I want to show how this is done in PDF format without using a tag library to display 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 possible using javascript or jquery?

Please help me with the code that I have in a few days, but get nothing.

+3
source share
5 answers

Using a mapping table in jsp would be pretty simple to convert it to * pdf along with .csv, .excel and son on. Here is a 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> 
+1
source

As far as I know, javascript cannot create pdf files on its own. And I haven't used struts yet. But I recommend you the Displaytag library, which is very easy to use :)

This is what you especially need ( with code ): http://displaytag.sourceforge.net/10/export.html

(from start to finish): http://displaytag.sourceforge.net/10/displaytag.pdf

+1
source

To generate a PDF from an HTML source in Java, you can use the iText HTMLWorker module (it is now deprecated, the new project is XMLWorker, but it depends on the version of iText you are using).

You can emulate the table you use on the JSP page in a String Action variable, say CreatePDFAction ;

Then, from the JSP, call CreatePDFAction with the submit button (if you want to open pdf on a new page).

In Struts.xml, declare the CreatePDFAction result as a stream result type with the corresponding contentType ( application/pdf ), and the desired contentDisposition to specify the file name and behavior: upload it ( attachment ) or open it in a browser ( inline ).

Inside the CreatePDFAction action CreatePDFAction you get a String, create a new document and a new HTMLWorker, feed it with a string containing your HTML, then extract the bytes from the resulting PDF and put it into the InputStream, getter action.

+1
source
  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(); } } 
+1
source

I am not sure about the location, I used itextpdf in JSP. http://tutorials.jenkov.com/java-itext/getting-started.html

Hopefuly will help

0
source

Source: https://habr.com/ru/post/1482008/


All Articles