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的新手。我想知道如何在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 page will be saved to PDF file).
(基本上,我希望在我的web应用程序中使用wkhtmltopdf,以便当用户单击save按钮时,当前页面将保存为PDF文件)。
3 个解决方案
#1
14
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.)
首先,technical note:因为你想使用wkhtmltopdf在web项目中,如果你部署Linux服务器机器通过ssh访问(在网络上),您需要使用修补Qt的版本,或者运行一个X服务器,如虚拟xvfb X服务器。(我不知道如果部署到运行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-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.
如果你只是想保存当前页面的注册服务器生成的版本,即没有任何变化可能已经像用户填写表单,或Javascript DOM添加新的元素,你只需要一个额外的可选参数?生成pdf =你的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 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.
另一种方法是有一个按钮“提交并生成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 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.
现在,问题仍然存在:当您的服务器具有必要的HTML(来自上述任何一种方法)时,如何将其提供给wkhtmltopdf?这是非常简单的。您将需要使用Runtime.getRuntime().exec()或名为ProcessBuilder的较新的API生成一个外部进程——请参见http://www.java-tips.org/java-se- tips/java.util/from-runtime.exec-builder.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 specifically you need to know.
这仅仅是一个大致的答案,它提供了一些指导。如果你需要更多的细节,请告诉我们你具体需要知道什么。
#2
8
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
如果你试图在一个叫wkhtmltopdf外部过程从java(或者,任何语言),请注意,你所看到的“正常”的输出在使用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代码,请参见链接。
#3
5
java-wkhtmltopdf-wrapper provides an easy API for using wkhtmltopdf
in Java.
Java -wkhtmltopdf-wrapper提供了在Java中使用wkhtmltopdf的简单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页面上的示例。
#1
14
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.)
首先,technical note:因为你想使用wkhtmltopdf在web项目中,如果你部署Linux服务器机器通过ssh访问(在网络上),您需要使用修补Qt的版本,或者运行一个X服务器,如虚拟xvfb X服务器。(我不知道如果部署到运行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-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.
如果你只是想保存当前页面的注册服务器生成的版本,即没有任何变化可能已经像用户填写表单,或Javascript DOM添加新的元素,你只需要一个额外的可选参数?生成pdf =你的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 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.
另一种方法是有一个按钮“提交并生成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 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.
现在,问题仍然存在:当您的服务器具有必要的HTML(来自上述任何一种方法)时,如何将其提供给wkhtmltopdf?这是非常简单的。您将需要使用Runtime.getRuntime().exec()或名为ProcessBuilder的较新的API生成一个外部进程——请参见http://www.java-tips.org/java-se- tips/java.util/from-runtime.exec-builder.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 specifically you need to know.
这仅仅是一个大致的答案,它提供了一些指导。如果你需要更多的细节,请告诉我们你具体需要知道什么。
#2
8
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
如果你试图在一个叫wkhtmltopdf外部过程从java(或者,任何语言),请注意,你所看到的“正常”的输出在使用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代码,请参见链接。
#3
5
java-wkhtmltopdf-wrapper provides an easy API for using wkhtmltopdf
in Java.
Java -wkhtmltopdf-wrapper提供了在Java中使用wkhtmltopdf的简单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页面上的示例。