Skip to content Skip to sidebar Skip to footer

How To Export Table Displayed On Jsp To Pdf In Java Strust2

This is my data display in table format. I want to show as it is in PDF without using display tag library of struts2. Copy

Solution 2:

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

Solution 3:

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.

Solution 4:

Finaly i got the solution here is the code


          <script language="javascript" type="text/javascript">
                 functionRetrivetable()
                {
                var table = document.getElementById("historyTable");

                if (table) {

                  // If outerHTML property available, use itif (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:submitonclick="Retrivetable()"value="Export to Pdf"action="ExportToPdf"method="ExportPDF"align="bottom"/>In the action classpublicStringExportPDF()
    {   
            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);
                        Documentdocument=newDocument(PageSize.LETTER);
                        PdfWriter pdfWriter = PdfWriter.getInstance(document, newFileOutputStream(filePath));
                           document.open();
                           HTMLWorker htmlWorker = newHTMLWorker(document);
                           htmlWorker.parse(newStringReader(tablestruct));
                            document.close();
                            System.out.println("Done");
                            File file = newFile(filePath);
                            inputStream = newDataInputStream( newFileInputStream(file));
                      }
                      catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                }     

Solution 5:

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

Hopefuly it will help

Post a Comment for "How To Export Table Displayed On Jsp To Pdf In Java Strust2"