RepeatHost Business Hosting

Reply to comment

C# Free Component to Generate PDF - Convert HTML to PDF

I was trying different components and methods of generating a PDF dynamically using C# and ASP.NET. There are quite a few pay-for

components with prices ranging between $250 and $1,000+ for a license. These pay-for products do a great job and some of them can

generate very complex PDF documents. But I just needed something that would generate simple PDF documents without too much in the way

of formatting.

There are also quite a few open source projects that provide rudimentary support for PDF creation. Many are pretty limited in their

feature sets. It seems Adobe has really kept it's power position by making sure their PDF format is as complex as possible. There is

one component in particular that I tried that worked quite well for my needs and is completely free. It's called Pdfizer. You can

view the site for this project here:

http://sourceforge.net/projects/pdfizer

It was a little difficult finding an up-to-date code sample. There is a post on Code Project on how to use this component but it was

dated 2004 that you can read here:

http://www.codeproject.com/KB/cs/pdfizer.aspx

Unfortunately, the code shown in this article is out-dated and won't even compile anymore. After some playing around and a few

different tries, I was able to get things working. To get this code to work, first download the latest build from sourceforge.net or

you can download the files here just in case the link for sourceforge.net is unavailable:

Pdfizer.zip

Generate PDF from HTML

First, set a reference in your project to the 3 DLL's that Pdfizer uses. Here are the 3 dll names to set a reference to:

ICSharpCode.SharpZipLib.dll (This component is used to parse the HTML)

itextsharp.dll (This component is used by Pdfizer to create the PDF document)

Pdfizer.dll (This is the main component with the HtmlToPdf object that executes the conversion operations).

Now we can add some code to use this component. Here is the code to generate a PDF from some HTML specified:

// set a path to where you want to write the PDF to.
string sPathToWritePdfTo = @"C:\new_pdf_name.pdf";
 
// build some HTML text to write as a PDF.  You could also 
// read this HTML from a file or other means.
// NOTE: This component doesn't understand CSS or other 
// newer style HTML so you will need to use depricated 
// HTML formatting such as the <font> tag to make it look correct.
System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();
sbHtml.Append("<html>");
sbHtml.Append("<body>");
sbHtml.Append("<font size='14'>My Document Title Line</font>");
sbHtml.Append("<br />");
sbHtml.Append("This is my document text");
sbHtml.Append("</body>");
sbHtml.Append("</html>");
 
// create file stream to PDF file to write to
using (System.IO.Stream stream = new System.IO.FileStream

(sPathToWritePdfTo, System.IO.FileMode.OpenOrCreate))
{
    // create new instance of Pdfizer
    Pdfizer.HtmlToPdfConverter htmlToPdf = new Pdfizer.HtmlToPdfConverter();
    // open stream to write Pdf to to
    htmlToPdf.Open(stream);
    // write the HTML to the component
    htmlToPdf.Run(sbHtml.ToString());
    // close the write operation and complete the PDF file
    htmlToPdf.Close();
}

This component also supports PDF Chapters. You could add a single line of code right before the Run() method to make the HTML specified a single chapter like this:

// open stream to write Pdf to to
htmlToPdf.Open(stream);
 
// add a chapter for this HTML
htmlToPdf.AddChapter("My Chapter Title 1");
 
// write the HTML to the component
htmlToPdf.Run(sbHtml.ToString());

Repeat the AddChapter() and Run() methods for each chapter you want to add and then Close() to commit it to the PDF.

Your PDF should look something like this:

sample pdfizer pdf format

Download PDF using ASP.NET

Once the PDF is created, you can dynamically stream it back to the client browser in ASP.NET on the fly as a file download using code like this:

// clear the http response so nothing else is in the stream so we can just isolate the file bits.
HttpContext.Current.Response.Clear();
// add the HTTP header to tell the browser to accept this as a file.  Also, the friendlypdfname.pdf is the name 
// of the PDF as you want it to appear to the user (regardless of what it is named in your file system).
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "friendlypdfname.pdf"));
// tell the browser what type of file this is so it can have a mime type associated with it.
HttpContext.Current.Response.ContentType = "application/pdf";
 
// pass the path that you wrote the file to on your file system as the parameter to WriteFile()

HttpContext.Current.Response.WriteFile(sPathToWritePdfTo);
// end the response and commit the file to the stream
HttpContext.Current.Response.End();

You could put this code in an OnClick() button event or other means that would then stream this new PDF down to your client browser.

Reply