Click here to Skip to main content
Click here to Skip to main content

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

, 17 Jan 2004
Rate this:
Please Sign up or sign in to vote.
This library converts simple HTML documents to PDF.


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
// start a chapter
html2pdf.AddChapter(@"Dummy Chapter");
string html = ...;
// convert string
// add a new chapter
html2pdf.AddChapter(@"Boost page");
// read web page
html2pdf.Run(new Uri(@""));
// close and finish pdf file.

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.


  1. NTidy, a .NET wrapper around Tidy.
  2. HTML Tidy home page.
  3. iTextSharp, PDF generation tool.


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
United States United States
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 (

Comments and Discussions

QuestionIs this usable??? or waste of time too Pinmembersimpa23-Aug-12 23:47 
AnswerRe: Is this usable??? or waste of time too Pinmembersimpa25-Aug-12 6:08 
BugHaving some exception error when using web content. Pinmemberjlopez78810-Jul-12 10:14 
GeneralRe: Having some exception error when using web content. Pinmembernarendra jarad21-Sep-12 22:56 
GeneralMy vote of 5 Pinmembermanoj kumar choubey16-Feb-12 0:04 
QuestionI couldnt convert the code, please help me with this. Convert the code to visual studio 2005 and Frameword 2.0 and give it to me back. PinmemberArivazhagan @ Saran28-Sep-11 20:04 
QuestionHTML to PDF code in article PinmemberDov Miller12-Sep-11 5:05 
GeneralMessage Automatically Removed Pinmembermjanulaitis123418-Apr-10 17:17 
GeneralRe: My vote of 1 Pinmemberscott.leckie2-Sep-10 13:19 
GeneralRe: My vote of 1 PinmemberAnkur\m/25-Feb-11 21:42 
GeneralRe: My vote of 1 Pinmemberscott.leckie26-Feb-11 8:39 
QuestionHow do you write the pdf to disk? PinmemberLawrence Botley24-Jul-09 1:04 
QuestionThe specified module could not be found. (Exception from HRESULT: 0x8007007E) PinmemberNikhil Prajapati15-Mar-09 22:26 
AnswerRe: The specified module could not be found. (Exception from HRESULT: 0x8007007E) PinmemberPierre Liétar6-Jul-09 5:21 
BugRe: The specified module could not be found. (Exception from HRESULT: 0x8007007E) PinmemberJanBorup17-Nov-14 22:49 
GeneralHtml TO PDF Pinmembervinodkrebc6-Mar-09 20:56 
GeneralRe: Html TO PDF PinmemberFlorin Chivu23-Jan-11 22:15 
GeneralRe: Html TO PDF PinmemberKurfuerstendamm18-Nov-13 0:38 
GeneralImages support Pinmembermerlinox1-Dec-08 0:57 
GeneralWhen i run the code i am getting HTML code insted of page UI PinmemberRam432125-Nov-08 1:16 
QuestionHTML to PDF Convertd, when the project its throws following error... Pinmemberrdssiva13-Nov-08 2:07 
AnswerRe: HTML to PDF Convertd, when the project its throws following error... Pinmembersubho1007-Apr-11 19:35 
GeneralTABLE and DIV Support PinmemberGokhan Mamaci25-Oct-08 7:27 
QuestionNo Spport Farsi Language ITextSharp.dll Pinmembers_nazari@yahoo.com23-Oct-08 0:01 
GeneralNo Spport Farsi Language ITextSharp.dll Pinmemberfatemeh22046-Oct-08 4:44 
GeneralPdfizer Projects Pinmemberfatemeh22046-Oct-08 3:16 
GeneralPdfizer Projects Pinmemberm.jafari545-Oct-08 21:14 
GeneralSample project given is not working..Please help Pinmembersrinath g nath2-May-08 2:13 
Questionpdfize can't support chinese? Pinmembereclay19-Feb-08 17:55 
GeneralNTidy.dll PinmemberMember 13389527-Feb-08 4:45 
GeneralThe specified module could not be found. (Exception from HRESULT: 0x8007007E) Pinmembermr_aladddin21-Jan-08 1:37 
GeneralRe: The specified module could not be found. (Exception from HRESULT: 0x8007007E) PinmemberSameers (theAngrycodeR )2-Apr-08 6:31 
GeneralRe: The specified module could not be found. (Exception from HRESULT: 0x8007007E) [modified] Pinmembercnj12516-Sep-08 22:27 
GeneralRe: The specified module could not be found. (Exception from HRESULT: 0x8007007E) PinmemberSameers (theAngrycodeR )17-Sep-08 2:04 
GeneralRe: The specified module could not be found. (Exception from HRESULT: 0x8007007E) Pinmemberscarface2113-Apr-11 2:39 
Generalc# Pinmembermihaela13-Jan-08 4:39 
Questionabt pdfizer Pinmemberabinmaloth4u22-Nov-07 16:44 
AnswerRe: abt pdfizer PinmemberRavi Bhavnani22-Nov-07 17:01 
AnswerRe: abt pdfizer PinmemberASGuru1-Jun-10 4:32 
QuestionNTidy.dll Support of DotNetNUke Pinmembertariq software engineer19-Aug-07 22:07 
GeneralHTML to PDF Library for .NET Pinmemberwinnovative6-Jul-07 13:04 
GeneralRe: HTML to PDF Library for .NET PinmemberMember 710029-Jul-08 1:00 
GeneralRe: HTML to PDF Library for .NET Pinmemberpsinke17-Nov-08 1:18 
GeneralFile extension test Pinmemberdanneth21-Mar-07 5:58 
GeneralFont color Pinmembersejmik13-Mar-07 7:21 
Generalweb service URL PinmemberPingu2213-Feb-07 5:05 
GeneralError PinmemberMarco Delgado11-Jan-07 3:08 
Generalurl doesn't support local file PinmemberPerlDev16-Nov-06 9:50 
GeneralWidth Problem PinmemberAfzal Farooqui22-Jun-06 7:12 
QuestionProblems with any entities Pinmembernico.piqueras26-May-06 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150123.1 | Last Updated 18 Jan 2004
Article Copyright 2004 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid