如何申请风格,同时编写HTML到PDF [英] How to apply style while writing html to PDF

查看:136
本文介绍了如何申请风格,同时编写HTML到PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加HTML到PDF文件。我的HTML是DIV的形式和含有其中具有像边框的风格样式属性:2px的固体红色​​; ,但这种风格并不适用于我写的HTML PDF文件。

我用下面的code:

 文档的文档=新的文件(PageSize.A4_LANDSCAPE,0,0,30,65);
PdfWriter.GetInstance(文件,新的FileStream(使用Server.Mappath(/)+温度/+parsetest1.pdf,FileMode.Create));
document.Open();
字符串的htmlText =< D​​IV的风格=边界:4PX固冲'>欢迎及LT; / DIV>中;
StringReader ABC =新StringReader(的htmlText);
清单< iTextSharp.text.IElement>元素= iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(ABC,NULL);
的foreach(以元素对象项)
{
   document.Add((IElement)项);
}
document.Close();


解决方案

iText的不完全支持CSS。它变得更好,特别是在过去几个版本,但还远远没有完成。

如果你需要真正准确的HTML,PDF转换,我建议WKHTML2PDF。这是一个命令行应用程序,是基于WebKit。这是相当IM pressive ......它会运行脚本事件,这么好,它可以正确地呈现包含谷歌地图的页面。

I am Adding html to the pdf file. My html is in div form and that containing the style attribute which having the style like border:2px solid red; but this style is not applying to the html that i am writing to the pdf file.

i am using the following code :

Document document = new Document(PageSize.A4_LANDSCAPE, 0, 0, 30, 65);
PdfWriter.GetInstance(document, new FileStream(Server.MapPath("/") + "Temp/" + "parsetest1.pdf", FileMode.Create));
document.Open();
String htmlText = "<div style='border:4px solid red'>Welcome</div>";
StringReader abc = new StringReader(htmlText);
List<iTextSharp.text.IElement> elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(abc, null);
foreach (object item in elements)
{
   document.Add((IElement)item);
}
document.Close();

解决方案

iText doesn't fully support CSS. It's getting better, particularly in the last couple releases, but is far from complete.

If you need Really Accurate HTML-PDF conversion, I suggest WKHTML2PDF. It's a command line app that is WebKit based. It's quite impressive... it'll run script event, so well that it can properly render a page containing a google map.

这篇关于如何申请风格,同时编写HTML到PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆