使用 Java 将 HTML 文件转换为 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38924048/
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 HTML File to PDF Using Java
提问by Developer Guy
I am looking for a way to convert an HTML file to PDF using a Java library that is preferably free. I have done some searching online to look for tools to use, but haven't found a solution that sticks out (I have seen some mention of iText, but it looked like that would have a charge to use it). Is there an existing library that I can utilize to accomplish the conversion of HTML to PDF?
我正在寻找一种使用最好免费的 Java 库将 HTML 文件转换为 PDF 的方法。我在网上做了一些搜索来寻找要使用的工具,但还没有找到一个突出的解决方案(我已经看到一些提到 iText,但看起来使用它需要收费)。是否有现有的库可以用来完成 HTML 到 PDF 的转换?
回答by V_CODES
I have used javascript to print HTML table in my application.
我使用 javascript 在我的应用程序中打印 HTML 表格。
Below function executes on click of 'Print' button. I have 'printElement' function for printing.
单击“打印”按钮执行以下功能。我有用于打印的“printElement”功能。
$("#btnlwPrint").off('click').on('click',function() {
var cboVendorName ;
cboVendorName= $('#cboVendorName').combogrid('textbox').val();
var tbodylength=$('#ssgGrid tbody tr').length;
if (cboVendorName =="" || tbodylength <= 1){
$('#warPrintEmptyRow').modal('toggle');
} else {
$('#lWVendor').text(cboVendorName);
printElement(document.getElementById("printHeader"));
printElement(document.getElementById("ssgGrid"),true);
window.print();
}
});
//Below function prints the grid
function printElement(elem, append, delimiter) {
var domClone = elem.cloneNode(true);
var $printSection = document.getElementById("printSection");
if (!$printSection) {
var $printSection = document.createElement("div");
$printSection.id = "printSection";
document.body.appendChild($printSection);
}
if (append !== true) {
$printSection.innerHTML = "";
}
else if (append === true) {
if (typeof(delimiter) === "string") {
$printSection.innerHTML += delimiter;
}
else if (typeof(delimiter) === "object") {
$printSection.appendChlid(delimiter);
}
}
$printSection.appendChild(domClone);
}
回答by Developer Guy
UPDATE:
更新:
I ended up using Flying-Saucer from the Maven repo: https://mvnrepository.com/artifact/org.xhtmlrenderer/flying-saucer-pdf
我最终使用了来自 Maven 仓库的 Flying-Saucer:https: //mvnrepository.com/artifact/org.xhtmlrenderer/flying-saucer-pdf
It was very simple to get this to work for me, here is a method I created to use this:
让它为我工作非常简单,这是我创建的使用它的方法:
public static void generatePDF(String inputHtmlPath, String outputPdfPath)
{
try {
String url = new File(inputHtmlPath).toURI().toURL().toString();
System.out.println("URL: " + url);
OutputStream out = new FileOutputStream(outputPdfPath);
//Flying Saucer part
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(out);
out.close();
} catch (DocumentException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And here is the usage:
这是用法:
public static void main(String[] args){
String inputFile = "C:/Users/jrothst/Desktop/TestHtml.htm";
String outputFile = "C:/Users/jrothst/Desktop/TestPdf.pdf";
generatePDF(inputFile, outputFile);
System.out.println("Done!");
}
It worked very well to output the PDF and was very simple to use. It also handled the CSS in the html pretty well. Didn't use it for external CSS, but I believe that is possible too.
它可以很好地输出 PDF 并且使用起来非常简单。它还很好地处理了 html 中的 CSS。没有将它用于外部 CSS,但我相信这也是可能的。
回答by ieugen
You have a few options:
您有几个选择:
- openhtmltopdf- new code, still brewing, but has some great results
- Apache FOP- can convert XML, not HTML, but might be usefull
- itextthe older version (version 2)
- Wkhtmltopdf- can call it from Java via external process, used it with great success so far
- openhtmltopdf- 新代码,仍在酝酿中,但有一些不错的结果
- Apache FOP- 可以转换 XML,而不是 HTML,但可能有用
- itext旧版本(版本 2)
- Wkhtmltopdf- 可以通过外部进程从 Java 调用它,到目前为止使用它取得了巨大的成功
回答by yatheendra k v
Here is the complete conversion of html file to pdf file working example.
这是 html 文件到 pdf 文件工作示例的完整转换。
import com.itextpdf.text.Document;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.StringReader;
import org.jsoup.Jsoup;
public class Html2pdf2 {
private Html2pdf2() {}
public static String extractText(Reader reader) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(reader);
String line;
while ( (line=br.readLine()) != null) {
sb.append(line);
}
String textOnly = Jsoup.parse(sb.toString()).text();
return textOnly;
}
public final static void main(String[] args) throws Exception{
FileReader reader = new FileReader
("example.html");
try {
OutputStream file = new FileOutputStream(new File("D:\Test.pdf"));
Document document = new Document();
PdfWriter.getInstance(document, file);
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(ht));
document.close();
file.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("finished converting");
}
}