Click here to Skip to main content
6,292,426 members and growing! (10,493 online)
Email Password   helpLost your password?
Languages » C# » Samples     Intermediate

Pdfizer, a dumb HTML to PDF converter, in C#

By Jonathan de Halleux

This library converts simple HTML documents to PDF.
C#, Windows, .NET 1.0, .NET 1.1, ASP.NET, VS.NET2003, Dev
Posted:17 Jan 2004
Views:173,945
Bookmarked:106 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
15 votes for this article.
Popularity: 5.14 Rating: 4.37 out of 5
2 votes, 13.3%
1
1 vote, 6.7%
2
1 vote, 6.7%
3

4
11 votes, 73.3%
5

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jonathan de Halleux


Member
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Occupation: Engineer
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 57 (Total in Forum: 57) (Refresh)FirstPrevNext
QuestionThe specified module could not be found. (Exception from HRESULT: 0x8007007E) PinmemberNikhil Prajapati22:26 15 Mar '09  
GeneralHtml TO PDF Pinmembervinodkrebc20:56 6 Mar '09  
GeneralImages support Pinmembermerlinox0:57 1 Dec '08  
GeneralWhen i run the code i am getting HTML code insted of page UI PinmemberRam43211:16 25 Nov '08  
QuestionHTML to PDF Convertd, when the project its throws following error... Pinmemberrdssiva2:07 13 Nov '08  
GeneralTABLE and DIV Support PinmemberGokhan Mamaci7:27 25 Oct '08  
QuestionNo Spport Farsi Language ITextSharp.dll Pinmembers_nazari@yahoo.com0:01 23 Oct '08  
GeneralNo Spport Farsi Language ITextSharp.dll Pinmemberfatemeh22044:44 6 Oct '08  
GeneralPdfizer Projects Pinmemberfatemeh22043:16 6 Oct '08  
GeneralPdfizer Projects Pinmemberm.jafari5421:14 5 Oct '08  
GeneralSample project given is not working..Please help Pinmembersrinath g nath2:13 2 May '08  
Generalpdfize can't support chinese? Pinmembereclay17:55 19 Feb '08  
GeneralNTidy.dll PinmemberMember 13389524:45 7 Feb '08  
GeneralThe specified module could not be found. (Exception from HRESULT: 0x8007007E) Pinmembermr_aladddin1:37 21 Jan '08  
GeneralRe: The specified module could not be found. (Exception from HRESULT: 0x8007007E) PinmemberSameers (theAngrycodeR )6:31 2 Apr '08  
GeneralRe: The specified module could not be found. (Exception from HRESULT: 0x8007007E) [modified] Pinmembercnj12522:27 16 Sep '08  
GeneralRe: The specified module could not be found. (Exception from HRESULT: 0x8007007E) PinmemberSameers (theAngrycodeR )2:04 17 Sep '08  
Generalc# Pinmembermihaela4:39 13 Jan '08  
Questionabt pdfizer Pinmemberabinmaloth4u16:44 22 Nov '07  
AnswerRe: abt pdfizer PinmemberRavi Bhavnani17:01 22 Nov '07  
QuestionNTidy.dll Support of DotNetNUke Pinmembertariq software engineer22:07 19 Aug '07  
GeneralHTML to PDF Library for .NET Pinmemberwinnovative13:04 6 Jul '07  
GeneralRe: HTML to PDF Library for .NET PinmemberMember 710021:00 9 Jul '08  
GeneralRe: HTML to PDF Library for .NET Pinmemberpsinke1:18 17 Nov '08  
GeneralHTML To PDF Converter for .NET PinmemberFlorentin BADEA5:20 24 May '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Jan 2004
Editor: Smitha Vijayan
Copyright 2004 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project