简体   繁体   English

iTextSharp的替代品,可将HTML转换为PDF

[英]Alternatives to iTextSharp for Converting HTML to PDF

I want to convert an HTML file to PDF using iTextSharp . 我想使用iTextSharp将HTML文件转换为PDF。

I used this code . 我用了这段代码

I then did the following to convert it to pdf: 然后,我执行以下操作将其转换为pdf:

HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.Parse(new StringReader(results));
document.Close();

However, none of the CSS is parsed. 但是,没有CSS被解析。 Any alternatives? 有其他选择吗?

18 months ago, we exhaustibly tested most of the html-to-pdf converters available (paid and free). 18个月前,我们对大多数可用的html-to-pdf转换器(收费和免费)进行了详尽的测试。 In the end, the hands-down best html-to-pdf converter was a free, open-source program called wkhtmltopdf . 最后, 最实用的 html-to-pdf转换器是一个名为wkhtmltopdf的免费开源程序。

It is command-line-driven, uses the webkit rendering engine, and accurately (and more importantly, consistently) turns web pages into beautiful looking pdf's. 它是命令行驱动的,使用webkit渲染引擎,并且可以准确(更重要的是,始终如一)将网页转换为漂亮的pdf。 It also has a variety of useful command-line switches, gives you complete header/footer control, optionally creates table-of-contents pages, and runs very, very fast. 它还具有各种有用的命令行开关,可为您提供完整的页眉/页脚控制,还可以选择创建目录表页面,并且运行非常非常快。 AND -- because it's command line driven, it has the added bonus of easily being used for batch generation. AND-因为它是命令行驱动的,所以它具有易于用于批处理生成的额外好处。 You simply cannot go wrong. 您根本不会出错。

As a sidenote, this program also has a sister program (bundled in download) called wkhtmltoimage, which works exactly the same way, but converts webpages to a variety of image formats. 作为附带说明,该程序还有一个名为wkhtmltoimage的姊妹程序(下载时捆绑在一起),其工作原理完全相同,但是将网页转换为多种图像格式。 This has also been highly useful and we use this functionality for taking snapshots of our web-based tools when building proposals for jobs. 这也非常有用,我们在构建求职建议时使用此功能为基于Web的工具制作快照。

I'm know that I sound like a total fanboy, but I'm not. 我知道我听起来像是一个狂热的粉丝,但事实并非如此。 I'm simply someone that got so sick-and-tired of all the bullsh*t converters out there that I was doubly blown away when we found wkhtmltopdf. 我只是一个对所有的bulsh * t转换器都感到厌烦的人,当我们找到wkhtmltopdf时,我被双重震撼了。 I simply cannot recommend this little program enough. 我根本不能推荐这个小程序。

在替代方案方面,我只能推荐ABCpdf ,作为我们的Web应用程序之一,我们已成功使用ABCpdf将大量HTML文档转换为PDF。

it won't load it automatically. 它不会自动加载。 You need to do it manually as follow. 您需要按照以下说明手动进行操作。

 StyleSheet ss = new StyleSheet();
 ss.LoadStyle("body", "leading", "16,0");
 ss.LoadStyle("p", "style", "font-family:Times New Roman");

and then you use 然后你用

List<IElement> htmlToPDF = HTMLWorker.ParseToList(new StringReader("<font face=\"Times\">" + html + "</font>"), style);

foreach (IElement element in htmlToPDF ) {
    document.Add(element);
  }

however, only some styles work in itextsharp not all of them. 但是,只有部分样式可以在itextsharp中使用,而并非全部。

Actually you should try XMLWorker , a plugin for itextsharp. 实际上,您应该尝试使用XMLWorker ,它是itextsharp的插件。 It just released this month. 它刚刚在本月发布。 It can handle html much better than using only itextsharp. 它比仅使用itextsharp可以更好地处理html。

I think wkhtmltopdf project looks good as well, but I don't know if it will play nice with our production environment since web application is multi-thread program. 我认为wkhtmltopdf项目看起来也不错,但是我不知道它是否可以在我们的生产环境中正常工作,因为Web应用程序是多线程程序。

Better late then never but here's a pretty good example of how to do it, also explains what is possible and what is not: http://www.thiscouldbeuseful.com/2012/04/itextsharp-to-rescue-converting-html-to.html . 最好不要迟到,但是这是如何做的一个很好的例子,还说明了可行和不可行的方法: http : //www.thiscouldbeuseful.com/2012/04/itextsharp-to-rescue-converting-html- to.html

I prefer iTextSharp because imo it provides the most functionality. 我更喜欢iTextSharp,因为imo提供最多的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM