如何在java strust2中将jsp上显示的表导出为pdf
这是我以表格格式显示的数据。 我想在没有使用struts2的显示标签库的情况下以PDF格式显示。
Leave ID FROM DATE TO DATE DAYS REQUESTED APPROVER NOTES REMARK IS PLANNED REASON 270 12/27/12 12/29/12 2 Sagar s s true s Cancel History
是用javascript还是jquery?
请帮我一些代码,我用谷歌搜索了几天但什么都没得到。
在jsp上使用display table会很容易将它转换为* pdf以及.csv,.excel和son on,这是示例代码;
据我所知,不幸的是javascript本身无法创建pdf文件。 我还没有使用struts。 但我建议您使用Displaytag库,这是非常容易使用的:)
这是您特别需要的( 使用代码 ): http : //displaytag.sourceforge.net/10/export.html
文档(从头到尾): http : //displaytag.sourceforge.net/10/displaytag.pdf
要使用Java从HTML
源生成PDF
,您可以使用iText's HTMLWorker
模块(现已弃用,新项目是XMLWorker,但这取决于您使用的iText版本)。
您可以在Action的String变量中模拟JSP页面上的表,比如说CreatePDFAction
;
然后,从JSP中,使用提交按钮调用CreatePDFAction
(如果需要,可在新页面上打开pdf)。
在Struts.xml中,将CreatePDFAction
结果声明为stream
结果类型 ,具有相应的contentType
( application/pdf
),以及用于指定文件名和行为的所需contentDisposition
:下载它( attachment
)或在浏览器中打开它( inline
)。
在CreatePDFAction
操作中,您将收到String,实例化新文档和新HTMLWorker,使用包含HTML的字符串提供它,然后从生成的PDF中提取字节并将其放入通过操作通过getter公开的InputStream中。
Finaly i got the solution here is the code In the action class public String ExportPDF() { tablestruct = ""+tablestruct+""; //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(); } }
我不确定struts,我在JSP中使用了itextpdf
。 http://tutorials.jenkov.com/java-itext/getting-started.html
希望它会有所帮助