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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:53:12  来源:igfitidea点击:

Java: How can I generate a PDF file from a JSP JSF page?

javapdfjsficefaces

提问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/pdfdata 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/pdfFilter

回答by Bozho

You will probably need the following:

您可能需要以下内容:

  1. 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();
    
  2. Transform the captured content to pdf. See the Flying Saucerrenderer (and additionally - this thread)

  3. "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")

  1. 捕获页面的 (X)HTML 输出。这可以通过您的 servlet 中的以下代码完成:

    InputStream is = new URL("http://localhost/foo/page.jsf").openStream();
    
  2. 将捕获的内容转换为 pdf。请参阅飞碟渲染器(以及 -此线程

  3. “发送”生成的pdf。也就是说,只需将您的 pdf 写入(打印)到response.getOutputStream(),并设置一个 Content-Type 标题 -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