javascript 如何在java strust2中将jsp上显示的表格导出为pdf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13717743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:35:29  来源:igfitidea点击:

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

javajavascriptstruts2pdf-generationitext

提问by Sagar Mahalle

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

这是我以表格格式显示的数据。我想在不使用struts2的显示标签库的情况下以PDF格式显示。

<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?

javascript或jquery可以吗?

Please help me with some code I have googled it for a few days but get nothing.

请帮我一些代码,我在谷歌上搜索了几天但什么也没得到。

采纳答案by MAK

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 ;

在jsp上使用显示表将很容易将其转换为* pdf以及.csv、.excel和son on,这是示例代码;

<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> 

回答by Timur Aykut YILDIRIM

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 :)

据我所知,不幸的是 javascript 不能自己创建 pdf 文件。而且我还没有使用过struts。但我推荐你 Displaytag 库,它非常易于使用:)

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

这是您特别需要的(带有代码):http: //displaytag.sourceforge.net/10/export.html

documentation (from beginning to end) : http://displaytag.sourceforge.net/10/displaytag.pdf

文档(从头到尾):http: //displaytag.sourceforge.net/10/displaytag.pdf

回答by Andrea Ligios

To generate a PDFfrom an HTMLsource in Java, you can use iText's HTMLWorkermodule (now deprecated, the new project is XMLWorker, but this depends on the iText version you're using).

要生成一个PDFHTML在Java源代码,您可以使用iText's HTMLWorker模块(现在已经过时,新项目是XMLWorker,但这取决于你使用的iText的版本)。

You can emulate the table you have on JSP page in an String variable of an Action, let's say CreatePDFAction;

您可以在 Action 的 String 变量中模拟 JSP 页面上的表,例如CreatePDFAction

Then, from the JSP, call CreatePDFActionwith a submit button (opening the pdf on a new page, if you want).

然后,从 JSP 调用CreatePDFAction提交按钮(如果需要,可以在新页面上打开 pdf)。

In Struts.xml, declare CreatePDFActionresult as streamresult type, with the appropriate contentType(application/pdf), and the desired contentDispositionfor specifying the filename, and the behavior: download it (attachment) or open it in browser (inline).

在 Struts.xml 中,CreatePDFActionstream结果声明为结果类型,使用适当的contentType( application/pdf) 和所需contentDisposition的文件名,以及行为:下载它 ( attachment) 或在浏览器中打开它 ( inline)。

Inside the CreatePDFActionaction, 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.

CreatePDFAction动作内部,您接收字符串,实例化一个新文档和一个新的 HTMLWorker,将包含您的 HTML 的字符串提供给它,然后从生成的 PDF 中提取字节并将其放入由动作通过 getter 公开的 InputStream 中。

回答by Sagar Mahalle

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

回答by Andrew

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

我不确定 struts,我itextpdf在 JSP 中使用过。 http://tutorials.jenkov.com/java-itext/getting-started.html

Hopefuly it will help

希望它会有所帮助