Click here to Skip to main content
11,567,926 members (47,717 online)
Click here to Skip to main content

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

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

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

Share

About the Author

Jonathan de Halleux
Engineer
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 (http://research.microsoft.com/pex).

You may also be interested in...

Comments and Discussions

 
QuestionIs this usable??? or waste of time too Pin
simpa23-Aug-12 22:47
membersimpa23-Aug-12 22:47 
AnswerRe: Is this usable??? or waste of time too Pin
simpa25-Aug-12 5:08
membersimpa25-Aug-12 5:08 
BugHaving some exception error when using web content. Pin
jlopez78810-Jul-12 9:14
memberjlopez78810-Jul-12 9:14 
GeneralRe: Having some exception error when using web content. Pin
narendra jarad21-Sep-12 21:56
membernarendra jarad21-Sep-12 21:56 
GeneralMy vote of 5 Pin
manoj kumar choubey15-Feb-12 23:04
membermanoj kumar choubey15-Feb-12 23: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. Pin
Arivazhagan @ Saran28-Sep-11 19:04
memberArivazhagan @ Saran28-Sep-11 19:04 
QuestionHTML to PDF code in article Pin
Dov Miller12-Sep-11 4:05
memberDov Miller12-Sep-11 4:05 
GeneralMessage Automatically Removed Pin
18-Apr-10 16:17
membermjanulaitis123418-Apr-10 16:17 
GeneralRe: My vote of 1 Pin
scott.leckie2-Sep-10 12:19
memberscott.leckie2-Sep-10 12:19 
GeneralRe: My vote of 1 Pin
Ankur\m/25-Feb-11 20:42
memberAnkur\m/25-Feb-11 20:42 
GeneralRe: My vote of 1 Pin
scott.leckie26-Feb-11 7:39
memberscott.leckie26-Feb-11 7:39 
QuestionHow do you write the pdf to disk? Pin
Lawrence Botley24-Jul-09 0:04
memberLawrence Botley24-Jul-09 0:04 
QuestionThe specified module could not be found. (Exception from HRESULT: 0x8007007E) Pin
Nikhil Prajapati15-Mar-09 21:26
memberNikhil Prajapati15-Mar-09 21:26 
AnswerRe: The specified module could not be found. (Exception from HRESULT: 0x8007007E) Pin
Pierre Liétar6-Jul-09 4:21
memberPierre Liétar6-Jul-09 4:21 
BugRe: The specified module could not be found. (Exception from HRESULT: 0x8007007E) Pin
JanBorup17-Nov-14 21:49
memberJanBorup17-Nov-14 21:49 
GeneralHtml TO PDF Pin
vinodkrebc6-Mar-09 19:56
membervinodkrebc6-Mar-09 19:56 
GeneralRe: Html TO PDF Pin
Florin Chivu23-Jan-11 21:15
memberFlorin Chivu23-Jan-11 21:15 
GeneralRe: Html TO PDF Pin
Kurfuerstendamm17-Nov-13 23:38
memberKurfuerstendamm17-Nov-13 23:38 
GeneralImages support Pin
merlinox30-Nov-08 23:57
membermerlinox30-Nov-08 23:57 
GeneralWhen i run the code i am getting HTML code insted of page UI Pin
Ram432125-Nov-08 0:16
memberRam432125-Nov-08 0:16 
QuestionHTML to PDF Convertd, when the project its throws following error... Pin
rdssiva13-Nov-08 1:07
memberrdssiva13-Nov-08 1:07 
AnswerRe: HTML to PDF Convertd, when the project its throws following error... Pin
subho1007-Apr-11 18:35
membersubho1007-Apr-11 18:35 
GeneralTABLE and DIV Support Pin
Gokhan Mamaci25-Oct-08 6:27
memberGokhan Mamaci25-Oct-08 6:27 
QuestionNo Spport Farsi Language ITextSharp.dll Pin
s_nazari@yahoo.com22-Oct-08 23:01
members_nazari@yahoo.com22-Oct-08 23:01 
GeneralNo Spport Farsi Language ITextSharp.dll Pin
fatemeh22046-Oct-08 3:44
memberfatemeh22046-Oct-08 3:44 

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
Web03 | 2.8.150624.2 | Last Updated 18 Jan 2004
Article Copyright 2004 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid