Java:如何从 JSP JSF 页面生成 PDF 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1965913/
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
Java: How can I generate a PDF file from a JSP JSF page?
提问by user239161
I need to generate a PDF from a JSP JSF page. I have searched the net, but I didn't found any examples of how should I do this. I need to transform the whole page, or maybe only the charts that I have on that page.
我需要从 JSP JSF 页面生成 PDF。我在网上搜索过,但没有找到任何关于如何执行此操作的示例。我需要转换整个页面,或者可能只转换我在该页面上的图表。
P.S. I'm also using IceFaces.
PS 我也在使用 IceFaces。
回答by Mark Pope
As far as I know this isn't directly possible. You can use Jasper Reportsto generate a pdf on the server side. Or, you can use PrintPDFwhich is a firefox plugin to create one from the web browser.
据我所知,这不是直接可能的。您可以使用Jasper Reports在服务器端生成 pdf。或者,您可以使用PrintPDF它是一个 firefox 插件从网络浏览器创建一个。
回答by McDowell
The easiest way is probably to capture the HTMLusing a Filterand convert that to PDF using a suitable API, then return the application/pdf
data from the Filter
. The IceFaces partial-submit support can probably be used to capture a subset of the component tree output, but you'll probably have to study the details of the IceFaces HTTP requests to figure out how to leverage that.
最简单的方法可能是使用过滤器捕获 HTML并使用合适的 API 将其转换为 PDF,然后从. IceFaces 部分提交支持可能可用于捕获组件树输出的子集,但您可能必须研究 IceFaces HTTP 请求的详细信息才能弄清楚如何利用它。application/pdf
Filter
回答by Bozho
You will probably need the following:
您可能需要以下内容:
Capture the (X)HTML output of your page. This can be done via the following code in a servlet of yours :
InputStream is = new URL("http://localhost/foo/page.jsf").openStream();
Transform the captured content to pdf. See the Flying Saucerrenderer (and additionally - this thread)
"Send" the generated pdf. That is, simply write (print) your pdf to the
response.getOutputStream()
, and set a Content-Type header -response.setContentType("application/pdf")
回答by dboldureanu
This might be an answer. Read this article: Combine JSF Facelets and the Flying Saucer XHTML Renderer
这可能是一个答案。阅读这篇文章:结合 JSF Facelets 和 Flying Saucer XHTML Renderer