user1016313
user1016313

Reputation: 1314

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?

Upvotes: 0

Views: 11260

Answers (4)

Daniel Szabo
Daniel Szabo

Reputation: 7281

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.

Upvotes: 13

Aries51
Aries51

Reputation: 377

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.

Upvotes: 1

maxisam
maxisam

Reputation: 22745

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.

Upvotes: 2

isNaN1247
isNaN1247

Reputation: 18109

In terms of alternatives I can only recommend ABCpdf, which we're successfully using to convert large quantities of HTML documents into PDF as part of one of our web applications.

Upvotes: 2

Related Questions