使用 Java 将 HTML 文件及其图片和样式转换为 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4449989/
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
Convert an HTML file to a PDF with their pictures and styles using Java
提问by Tim Tuckle
Possible Duplicate:
Converting HTML Files to PDF
可能的重复:
将 HTML 文件转换为 PDF
I want to convert an HTML file to PDF by using Java. I've searched stackoverflow and other sites. I am suprised. Because I couldn't find any easy way.
我想使用 Java 将 HTML 文件转换为 PDF。我搜索过 stackoverflow 和其他网站。我很惊讶。因为我找不到任何简单的方法。
Could you help me with to do that?
你能帮我做吗?
Thanks in advance.
提前致谢。
回答by AlexR
回答by Steve McLeod
If there is no Java library to do this, I suggest finding a command-line tool that does the job, and invoking it from Java using Runtime.exec(). Not ideal though.
如果没有 Java 库来执行此操作,我建议找到一个可以完成这项工作的命令行工具,并使用 Runtime.exec() 从 Java 中调用它。虽然不理想。
回答by bharath
here is the sample to do that
这是执行此操作的示例
import officetools.OfficeFile; // this is my tools package
...
FileInputStream fis = new FileInputStream(new File("test.html"));
FileOutputStream fos = new FileOutputStream(new File("test.pdf"));
// suppose OpenOffice.org runs on localhost, port 8100
OfficeFile f = new OfficeFile(fis,"localhost","8100", true);
f.convert(fos,"pdf");
...