java iText 样式将 HTML 解析为 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13816429/
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
iText style parsing HTML to PDF
提问by CeccoCQ
I've a problem with iText.
我有 iText 的问题。
I've followed this link: How to export html page to pdf format?
我已经点击了这个链接:如何将 html 页面导出为 pdf 格式?
My snippet:
我的片段:
String str = "<html><head><body><div style=\"width:100%;height:100%;\"><h3 style=\"margin-left:5px;margin-top:40px\">First</h3><div style=\"margin-left:15px;margin-top:15px\"><title></title><p>sdasdasd shshshshdffgdfgd</p></div><h3 style=\"margin-left:5px;margin-top:40px\">The dream</h3><div style=\"margin-left:15px;margin-top:15px\"></div></div></body></head></html>";
String fileNameWithPath = "/Users/cecco/Desktop/pdf2.pdf";
com.itextpdf.text.Document document =
new com.itextpdf.text.Document(com.itextpdf.text.PageSize.A4);
FileOutputStream fos = new FileOutputStream(fileNameWithPath);
com.itextpdf.text.pdf.PdfWriter pdfWriter =
com.itextpdf.text.pdf.PdfWriter.getInstance(document, fos);
document.open();
document.addAuthor("Myself");
document.addSubject("My Subject");
document.addCreationDate();
document.addTitle("My Title");
com.itextpdf.text.html.simpleparser.HTMLWorker htmlWorker =
new com.itextpdf.text.html.simpleparser.HTMLWorker(document);
htmlWorker.parse(new StringReader(str.toString()));
document.close();
fos.close();
and work fine.
并且工作正常。
But tag style into h3 and div aren't considered.
但是不考虑将样式标记为 h3 和 div。
But if I copy my html into http://htmledit.squarefree.com/all is correct.
但是如果我将我的 html 复制到http://htmledit.squarefree.com/ 中,一切都是正确的。
How can I solve this problem?
我怎么解决这个问题?
回答by ollo
iText isn't the best Html Parser, but you can use Flying-Saucerfor this. Flying-Saucer is build on top of iText but has a capable Xml / (X)Html parser. Short: Flying Saucer is perfect if you want html -> Pdf.
iText 不是最好的 Html 解析器,但您可以为此使用Flying-Saucer。Flying-Saucer 构建在 iText 之上,但具有功能强大的 Xml / (X)Html 解析器。简短:如果你想要 html -> Pdf,飞碟是完美的。
Here's how to generate the pdf from your string:
以下是从字符串生成 pdf 的方法:
/*
* Note: i filled something in the title-tag and fixed the head tag (the whole body-tag was in the head)
*/
String str = "<html><head></head><body><div style=\"width:100%;height:100%;\"><h3 style=\"margin-left:5px;margin-top:40px\">First</h3><div style=\"margin-left:15px;margin-top:15px\"><title>t</title><p>sdasdasd shshshshdffgdfgd</p></div><h3 style=\"margin-left:5px;margin-top:40px\">The dream</h3><div style=\"margin-left:15px;margin-top:15px\"></div></div></body></html>";
OutputStream os = new FileOutputStream(new File("example.pdf"));
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(str);
renderer.layout();
renderer.createPDF(os);
os.close();
But:FS supports only validHtml / Xhtml / xml, so make shure it is.
但是:FS 只支持有效的Html/Xhtml/xml,所以确保它是。