eclipse 如何在 Java Web 应用程序中使用 wkhtmltopdf?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5688585/
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-09-19 16:11:30  来源:igfitidea点击:

How to use wkhtmltopdf in Java web application?

javaeclipseweb-applicationsjakarta-eewkhtmltopdf

提问by Mellon

I am newbie in wkhtmltopdf. I am wondering how to use wkhtmltopdf with my Dynamic Web Projectin Eclipse? How to integrate wkhtmltopdf with my Java dynamic web application?

我是wkhtmltopdf 的新手。我想知道如何在 Eclipse中将 wkhtmltopdf 与我的动态 Web 项目一起使用?如何将 wkhtmltopdf 与我的 Java 动态 Web 应用程序集成?

Is there any tutorials available for beginners of wkhtmltopdf ?

有没有适合 wkhtmltopdf 初学者的教程?

(Basically, I would like to use wkhtmltopdf in my web application so that when user click a save button , the current pagewill be saved to PDF file).

(基本上,我想在我的网络应用程序中使用 wkhtmltopdf 以便当用户单击保存按钮时,当前页面将被保存为 PDF 文件)。

回答by Robin Green

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

首先,技术说明:因为你想在 web 项目中使用 wkhtmltopdf,如果你部署到你通过 ssh(即通过网络)访问的 Linux 服务器机器,你将需要使用修补过的 Qt 版本,或者运行一个 X 服务器,例如虚拟 X 服务器 xvfb。(我不知道如果部署到运行 Linux 以外的操作系统的服务器会发生什么。)

Second, it should be really quite simple to use wkhtmltopdf from any language in a web project.

其次,在 web 项目中使用任何语言的 wkhtmltopdf 应该非常简单。

If you just want to save the server-generatedversion 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=pdfon 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.

如果您只想保存当前页面的服务器生成版本,即没有任何更改,例如用户填写表单或 Javascript 添加新的 DOM 元素,您只需要一个额外的可选参数,如?generate=pdfon URL 的末尾,这将导致该页面生成为 PDF,然后 PDF 按钮将链接到该 URL。如果您只是使用简单的 JSP 或其他东西,手动添加到每个页面可能需要大量工作,但是根据您使用的 Web 框架,Web 框架可能会提供一些帮助以在每个页面上实现相同的操作,如果你需要实现它。

To implement this approach, you would probably want to capture the response by wrapping the response object and overridding its getWriter()and getOutputStream()methods.

要实现这种方法,您可能希望通过包装响应对象并覆盖其getWriter()getOutputStream()方法来捕获响应。

Another approach is to have a button "submit and generate PDF" which will generate the nextpage 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.

另一种方法是有一个按钮“提交和生成PDF”,这将产生下一个页面的PDF。如果您有用户需要填写的表单,这可能更有意义 - 我不知道。这真的是一个设计决定。

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

第三种方法是使用 Javascript 将页面的当前状态上传回服务器,并使用 wkhtmltopdf 进行处理。这将适用于任何页面。(这甚至可以在任何网站上使用,而不仅仅是您的网站,如果您将其制作为书签。这只是我想到的一个想法 - 这可能不是一个好主意。)

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.

第四种方法是,因为 wkhtmltopdf 可以获取 URL,传递您页面的 URL 而不是页面的内容(这仅在请求是 HTTP GET 时才有效,或者它等效于同一 URL 上的 HTTP GET )。这比捕获您自己的响应输出有一些小开销,但它可能可以忽略不计。您还很可能需要使用这种方法将 cookie 复制到 cookie jar 中,因为您的用户可能已登录或具有隐式会话。

So as you can see there are quite a lot of choices!

所以正如你所看到的,有很多选择!

Now, the question remains: when your server hasthe 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 called ProcessBuilder- see http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.htmlfor a comparison. If you are smart about it you should be able to do this without needing to create any temporary files.

现在,问题仍然存在:当您的服务器具有必要的 HTML 时,通过上述任何一种方法,如何将其输入 wkhtmltopdf?这很简单。您将需要使用Runtime.getRuntime().exec()或新的 API生成外部进程ProcessBuilder- 请参阅http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder。 html进行比较。如果你很聪明,你应该能够做到这一点,而无需创建任何临时文件。

One of the wkhtmltopdf websites is currently down, but the main README is available here, which explains the command line arguments.

wkhtmltopdf 网站之一目前已关闭,但此处提供了主要自述文件,其中解释了命令行参数。

This is merely an outline answer which gives some pointers. If you need more details, let us know what specificallyyou need to know.

这只是一个大纲答案,可以提供一些指导。如果您需要更多详细信息,请告诉我们您具体需要了解的内容。

回答by Riyas Valiya

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

如果您最终尝试从 java(或就此而言,任何语言)在外部进程中调用 wkhtmltopdf,请注意从命令行使用 wkhtmltopdf 时您看到的“正常”输出(即您期望的输出)在 STDOUT 中看到)不是在 STDOUT 中,而是在 STDERR 中。我在项目页面中提出了这个问题

http://code.google.com/p/wkhtmltopdf/issues/detail?id=825

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.

并回答说这是设计使然,因为 wkhtmltopdf 支持在 STDOUT 中提供实际的 pdf 输出。请参阅链接以获取更多详细信息和 java 代码。

回答by Dario Seidl

java-wkhtmltopdf-wrapperprovides an easy API for using wkhtmltopdfin Java.

java-wkhtmltopdf-wrapper提供了一个wkhtmltopdf在 Java 中使用的简单 API 。

It also works out-of-the-box on a headless server with xvfb.

它还可以在带有xvfb.

E.g., on a Ubuntu or Debian server: aptitude install wkhtmltopdf xvfb

例如,在 Ubuntu 或 Debian 服务器上: aptitude install wkhtmltopdf xvfb

Then in Java:

然后在Java中:

Pdf pdf = new Pdf();
pdf.addPage("http://www.google.com", PageType.url);
pdf.saveAs("output.pdf");

See the examples on their Github page for more options.

有关更多选项,请参阅其 Github 页面上的示例。