如何在Java Web应用程序中使用wkhtmltopdf?
我是 wkhtmltopdf 的新手。我想知道如何在 Eclipse 中将 wkhtmltopdf 与我的动态 Web 项目结合使用?如何将 wkhtmltopdf 与我的 Java 动态 Web 应用程序集成?
有没有适合 wkhtmltopdf 初学者的教程?
(基本上,我想在我的网络应用程序中使用 wkhtmltopdf,以便当用户单击保存按钮时,当前页面将被保存到 PDF 文件)。
I am newbie in wkhtmltopdf. I am wondering how to use wkhtmltopdf with my Dynamic Web Project in Eclipse? How to integrate wkhtmltopdf with my Java dynamic web application?
Is there any tutorials available for beginners of wkhtmltopdf ?
(Basically, I would like to use wkhtmltopdf in my web application so that when user click a save button , the current page will be saved to PDF file).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,技术说明:因为您想在 Web 项目中使用 wkhtmltopdf,所以如果您部署到通过 ssh(即通过网络)访问的 Linux 服务器计算机,您将需要使用修补的 Qt 版本,或者运行一个 X 服务器,例如虚拟 X 服务器 xvfb。 (我不知道如果部署到运行 Linux 以外的操作系统的服务器上会发生什么。)
其次,在 Web 项目中从任何语言使用 wkhtmltopdf 都应该非常简单。
如果您只想保存当前页面的服务器生成版本,即没有进行任何更改,例如用户填写表单或Javascript添加新的DOM元素,您只需要在 URL 末尾有一个额外的可选参数,例如
?generate=pdf
,这将导致该页面生成为 PDF,然后 PDF 按钮将链接到该 URL。如果您只是使用简单的 JSP 或其他东西,那么手动添加到每个页面可能需要做很多工作,但是根据您使用的 Web 框架,Web 框架可能会提供一些帮助来在每个页面上实现相同的操作,如果你需要实施它。要实现此方法,您可能希望通过包装响应对象并重写其
getWriter()
和getOutputStream()
方法来捕获响应。另一种方法是使用“提交并生成 PDF”按钮,它将下页面生成为 PDF。如果您有用户需要填写的表格,这可能更有意义 - 我不知道。这确实是一个设计决定。
第三种方法是使用 Javascript 将页面的当前状态上传回服务器,并使用 wkhtmltopdf 进行处理。这适用于任何页面。 (这甚至可以在任何网站上使用,而不仅仅是您的网站,如果您将其设为书签。这只是我想到的一个想法 - 这可能不是一个好主意。)
第四种方法是,因为 wkhtmltopdf 可以获取 URL,传递页面的 URL 而不是页面的内容(只有当请求是 HTTP GET,或者相当于同一 URL 上的 HTTP GET 时,这才有效)。与捕获您自己的响应输出相比,这会产生一些少量的开销,但它可能可以忽略不计。您还很可能需要使用此方法将 cookie 复制到 cookie jar 中,因为您的用户可能已登录或具有隐式会话。
正如您所看到的,有很多选择!
现在,问题依然存在:当您的服务器拥有通过上述任何方法获得的必要 HTML 时,如何将其输入 wkhtmltopdf 中?这很简单。您需要使用
Runtime.getRuntime().exec()
或名为ProcessBuilder
的较新 API 生成外部进程 - 请参阅 http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html进行比较。如果您很聪明,您应该能够做到这一点,而无需创建任何临时文件。wkhtmltopdf 网站之一目前已关闭,但主要自述文件可在此处获取,它解释了命令行参数。
这只是一个大纲答案,提供了一些指导。如果您需要更多详细信息,请告诉我们您具体需要了解的内容。
First, a technical note: Because you want to use wkhtmltopdf in a web project, if and when you deploy to a Linux server machine that you access via ssh (i.e. over the network), you will need to either use the patched Qt version, or run an X server, e.g. the dummy X server xvfb. (I don't know what happens if you deploy to a server running an operating system other than Linux.)
Second, it should be really quite simple to use wkhtmltopdf from any language in a web project.
If you just want to save the server-generated version of the current page, i.e. without any changes which might have been made like the user filling on forms, or Javascript adding new DOM elements, you just need to have an extra optional argument like
?generate=pdf
on the end of your URL, which will cause that page to be generated as a PDF, and then the PDF button will link to that URL. This may be a lot of work to add to each page manually if you are just using simple JSP or something, but depending on which web framework you are using, the web framework may offer some help to implement the same action on every page, if you need to implement that.To implement this approach, you would probably want to capture the response by wrapping the response object and overridding its
getWriter()
andgetOutputStream()
methods.Another approach is to have a button "submit and generate PDF" which will generate the next page as a PDF. This might make more sense if you have a form the user needs to fill in - I don't know. It's a design decision really.
A third approach is to use Javascript to upload the current state of the page back to the server, and process that using wkhtmltopdf. This will work on any page. (This can even be used on any site, not just yours, if you make it a bookmarklet. Just an idea that occurred to me - it may not be a good idea.)
A fourth approach is, because wkhtmltopdf can fetch URLs, to pass the URL of your page instead of the contents of the page (which will only work if the request was a HTTP GET, or if it's equivalent to a HTTP GET on the same URL). This has some small amount of overhead over capturing your own response output, but it will probably be negligible. You will also very likely need to copy the cookie(s) into a cookie jar with this approach, since presumably your user might be logged in or have an implicit session.
So as you can see there are quite a lot of choices!
Now, the question remains: when your server has the necessary HTML, from any of the above approaches, how to feed it into wkhtmltopdf? This is pretty simple. You will need to spawn an external process using either
Runtime.getRuntime().exec()
, or the newer API calledProcessBuilder
- see http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html for a comparison. If you are smart about it you should be able to do this without needing to create any temporary files.One of the wkhtmltopdf websites is currently down, but the main README is available here, which explains the command line arguments.
This is merely an outline answer which gives some pointers. If you need more details, let us know what specifically you need to know.
附加信息:
如果您最终尝试在 java(或任何语言)的外部进程中调用 wkhtmltopdf,请注意,从命令行使用 wkhtmltopdf 时您看到的“正常”输出(即您的输出)期望在 STDOUT 中看到)不是不在 STDOUT 中而是在 STDERR 中。我在项目页面
http://code.google.com 中提出了此问题/p/wkhtmltopdf/issues/detail?id=825
并被回复说这是设计使然,因为 wkhtmltopdf 支持在 STDOUT 中给出实际的 pdf 输出。请参阅链接了解更多详细信息和 java 代码。
Additional info:
If you do end up trying to call wkhtmltopdf in an external process from java (or for that matter, any language), please note that the "normal" output that you see when using wkhtmltopdf from the command line (i.e. what you would expect to see in STDOUT) is not not in STDOUT but in STDERR. I raised this issue in the project page
http://code.google.com/p/wkhtmltopdf/issues/detail?id=825
and was replied that this is by design because wkhtmltopdf supports giving the actual pdf output in STDOUT. Please see the link for more details and java code.
java-wkhtmltopdf-wrapper 提供了一个简单的 API 来使用
wkhtmltopdf
在爪哇。它还可以在带有
xvfb
的无头服务器上开箱即用。例如,在 Ubuntu 或 Debian 服务器上:
aptitude install wkhtmltopdf xvfb
然后在 Java 中:
请参阅其 Github 页面上的示例以获取更多选项。
java-wkhtmltopdf-wrapper provides an easy API for using
wkhtmltopdf
in Java.It also works out-of-the-box on a headless server with
xvfb
.E.g., on a Ubuntu or Debian server:
aptitude install wkhtmltopdf xvfb
Then in Java:
See the examples on their Github page for more options.