简体   繁体   中英

Alternatives to iTextSharp for Converting HTML to PDF

I want to convert an HTML file to PDF using iTextSharp .

I used this code .

I then did the following to convert it to pdf:

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

However, none of the CSS is parsed. Any alternatives?

18 months ago, we exhaustibly tested most of the html-to-pdf converters available (paid and free). In the end, the hands-down best html-to-pdf converter was a free, open-source program called 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. 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. 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. This has also been highly useful and we use this functionality for taking snapshots of our web-based tools when building proposals for jobs.

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. 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.

Actually you should try XMLWorker , a plugin for itextsharp. It just released this month. It can handle html much better than using only itextsharp.

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.

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 .

I prefer iTextSharp because imo it provides the most functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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