Introduction
This article presents a basic HTML to PDF converter: with this library, you can transform simple HTML pages to nice and printable PDF files.
The HTML cleaning is done with NTidy (see [1]), a .NET wrapper for the HTML Tidy library (see [2]). The PDF generation is done with iTextSharp, a PDF generation library (see [3]).
Transformation Pipe
Transforming HTML documents to PDF is a fairly complex task. Hopefully, there exists powerful tools on the web that could help me accomplish this.
Parsing HTML
The first problem to handle was that HTML is usually “dirty”: the structure is usually not XML conformant and trying to parse HTML pages with the XmlDocument will usually lead to a failure.
To overcome this problem, I had to write a .NET wrapper around HTML Tidy (see [2]). HTML Tidy is a very useful application that takes “dirty” HTML and returns it cleaned as much as possible. The .NET wrapper exposes a DOM-like class structure so that you can use it much like XmlDocument.
Hence, with NTidy, we can safely parse HTML document.
Creating PDF
The PDF creation is done by iTextSharp (see [3]), a .NET library hosted on SourceForge, that gives you the tool to create PDF easily. Hence, the PDF creation problem is solved.
Reading, Traversing
With NTidy and iTextSharp on my toolset, I could start to create the generator. The generator works like this: it first reads the input with NTidy, then traverses the DOM tree and generates the PDF fragments with iTextSharp.
Quick Example
The library usage is done through the HtmlToPdfConverter class. Creating a PDF file is done through the following steps, as illustrated in the example:
1. Create a converter,
2. Open a new PDF file using the Open method,
3. Add a chapter,
4. Feed HTML to the converter,
5. If you want another chapter, go to 3.
6. When finished, close the PDF file by calling Close.
// create converter
HtmlToPdfConverter html2pdf = new HtmlToPdfConverter();
// open new pdf file
html2pdf.Open(@"test");
// start a chapter
html2pdf.AddChapter(@"Dummy Chapter");
string html = ...;
// convert string
html2pdf.Run(html);
// add a new chapter
html2pdf.AddChapter(@"Boost page");
// read web page
html2pdf.Run(new Uri(@"http://www.boost.org/libs/libraries.htm"));
// close and finish pdf file.
html2pdf.Close();
What to expect and not expect
Don’t expect too much from this tool, it will not work with complex HTML pages and will give fairly good results with simple HTML pages. Specially, tables are not yet supported.
Reference
1. NTidy, a .NET wrapper around Tidy.
2. HTML Tidy home page.
3. iTextSharp, PDF generation tool.
From: http://www.codeproject.com/csharp/pdfizer.asp
Please download the new code from http://sourceforge.net/projects/pdfizer/
Hi there,
I am looking for the component (dll) for my web application (c#).
Required Features
1.pdf form fill on fly.
2.merge pdfs on fly.
3.html content in pdf (html to pdf conversion).
Please let me know if you have any dll which satify my requirements.
thanks,
Naveen Vadada
I have give you mail, thanks in advance.
I have the frist two features and the 3rd not including all the html tag.
Will this solution render jpgs or gifs that are included in the html?
yes, it supports images in html.
I have to place some hml tags ino a pdf file. how can I?
I am not sure I catch your mean, do you just want to put some html tags as normal text in PDF, right?
Many 3rd party DLLs are giving first two features. But I am mainly looking for 3rd one, means I have one aspx page which need to display as pdf (My page has HTML tags).
Please let me know if you have any DLL which support above requirement.
Thanks,
Naveen Vadada