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

后端 未结 5 1274
梦如初夏
梦如初夏 2020-12-18 10:56

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

相关标签:
5条回答
  • 2020-12-18 11:45

    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

    0 讨论(0)
  • 2020-12-18 11:50
            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();
                            }
                    }     
    
    0 讨论(0)
  • 2020-12-18 11:52

    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.

    0 讨论(0)
  • 2020-12-18 11:53

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

    Hopefuly it will help

    0 讨论(0)
  • 2020-12-18 11:56

    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> 
    
    0 讨论(0)
提交回复
热议问题