如何在Java Web应用程序中使用wkhtmltopdf? [英] How to use wkhtmltopdf in Java web application?

查看:134
本文介绍了如何在Java Web应用程序中使用wkhtmltopdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

是否有适用于wkhtmltopdf初学者的教程?

Is there any tutorials available for beginners of wkhtmltopdf ?

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

(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项目中使用wkhtmltopdf,当你部署到Linux时您通过ssh(即通过网络)访问的服务器机器,您将需要使用修补的Qt版本,或运行X服务器,例如虚拟X服务器xvfb。 (我不知道如果你部署到运行Linux以外的操作系统的服务器会发生什么。)

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

其次,使用wkhtmltopdf非常简单Web项目中的任何语言。

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

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

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.

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

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

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

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.

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

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

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

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!

现在,问题仍然存在:当你的服务器拥有必要的HTML时,来自任何上述方法,如何将其提供给wkhtmltopdf?这很简单。您需要使用 Runtime.getRuntime()。exec()或更新的名为 ProcessBuilder - 请参阅 http ://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html 进行比较。如果你很聪明,你应该能够这样做而不需要创建任何临时文件。

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 called ProcessBuilder - 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.

其中一个wkhtmltopdf网站目前正在关闭,但主要的自述文件是此处,解释命令行参数。

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 Web应用程序中使用wkhtmltopdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆