[英]iText style parsing HTML to PDF
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? 我怎么解决这个问题?
iText isn't the best Html Parser, but you can use Flying-Saucer for this. iText不是最好的HTML解析器,但是您可以为此使用Flying-Saucer 。 Flying-Saucer is build on top of iText but has a capable Xml / (X)Html parser.
Flying-Saucer是在iText之上构建的,但具有功能强大的Xml /(X)Html解析器。 Short: Flying Saucer is perfect if you want html -> Pdf.
简短:如果需要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 valid Html / Xhtml / xml, so make shure it is. 但是: FS仅支持有效的 HTML / Xhtml / xml,因此请确保它是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.