You are on page 1of 6

Username   Password

        

Forgot Username or Password?

Search

HOME BLOG PRODUCTS BUY CONSULTING HOSTING SUPPORT

C# Free Component To Generate PDF - Convert HTML To PDF Popular Articles

ASP.NET C# pdf
Fixing Relative Paths in C# ASP.NET When
Using Url Rewriting

I was trying different components and methods of generating a PDF dynamically using C# and ASP.NET. There are Building a Web Service in ASP.NET 3.5
quite a few pay-for ASP.NET Forms Authentication Simplified

components with prices ranging between $250 and $1,000+ for a license. These pay-for products do a great job and C# Free Component to Generate PDF -
some of them can Convert HTML to PDF

CSS File Include Order


generate very complex PDF documents. But I just needed something that would generate simple PDF documents
Using a Remote Terminal Licensing Server
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 Recent Comments
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 Aha! Problems with UpdatePanel? Sorted ...
possible. There is 1 day 2 hours ago

Some help required with this


one component in particular that I tried that worked quite well for my needs and is completely free. It's called Pdfizer.
1 day 8 hours ago
You can
Never seen this issue
view the site for this project here: 1 week 1 day ago
Demo Version - Winnovative Software ComponentsError in query.ToList()
http://sourceforge.net/projects/pdfizer
1 week 1 day ago
It was a little difficult finding an up-to-date code sample. There is a post on Code Project on how to use this
Thanks
component but it was
1 week 2 days ago

dated 2004 that you can read here: Thanks


1 week 5 days ago
http://www.codeproject.com/KB/cs/pdfizer.aspx
To get the data working,
Unfortunately, the code shown in this article is out-dated and won't even compile anymore. After some playing around 2 weeks 1 day ago
and a few
If I manually change the
2 weeks 1 day ago
different tries, I was able to get things working. To get this code to work, first download the latest build from
sourceforge.net or Handling EDM Relationship Metadata
2 weeks 1 day ago
you can download the files here just in case the link for sourceforge.net is unavailable:
About the itextsharp version
2 weeks 1 day ago
Pdfizer.zip

Generate PDF from HTML

Similar
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) Fixing Relative Paths in C# ASP.NET
When Using Url Rewriting
itextsharp.dll (This component is used by Pdfizer to create the PDF document)
How to Highlight the Day in the ASP.NET
Calendar Control with the SelectedDate
Pdfizer.dll (This is the main component with the HtmlToPdf object that executes the conversion operations).
Property
Now we can add some code to use this component. Here is the code to generate a PDF from some HTML specified: Global.asax Events in IIS 6 and IIS 7 for
Static Resources
// set a path to where you want to write the PDF to.
Create a Windows-style GroupBox in
string sPathToWritePdfTo = @"C:\new_pdf_name.pdf";
  ASP.NET
// build some HTML text to write as a PDF. You could also Changing the Application Pool Identity
// read this HTML from a file or other means. with Windows Server 2008 and IIS 7
// 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:

Demo Version - Winnovative Software Components

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.

Add new comment


Some help required with this
Submitted by Anonymous on Wed, 09/08/2010 - 02:05.

I'm trying to include a html table with this. The table appears fine, but it comes with a wide margin on either side
of the page, this is making the table too cramped. How can I make the table spread through the width of the pdf
page? Please help!

KD

reply

About the itextsharp version


Submitted by Anonymous on Wed, 08/25/2010 - 02:14.

Hi, this was a very helpful post! However, I already have a version of itextsharp.dll (4.0.2.0), and the one available
with this component is version 1.0.4.0. I need to use the 4.0 version in my application, and I need to find a way to
get this component to refer the 4.0 dll and not look for the 1.0.4 version. How do I do this? Any help would be
welcome.

I get this error now - Could not load file or assembly 'itextsharp, Version=1.0.4.0, Culture=neutral,
PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match
the assembly reference. (Exception from HRESULT: 0x80131040)

Thanks
KD
reply

medyum
Submitted by Anonymous on Tue, 08/17/2010 - 05:44.

I am to a great extent impressed with the article I have just read interesting very good
reply

Hi...I am passing html file


Submitted by Anonymous on Fri, 07/30/2010 - 22:24.

Hi...I am passing html file which contains pictures too..Its giving an error that Data at the root level is invalid. Line
1, position 1..what does this mean?
reply

Page Break Demo Version - Winnovative Software Components


Submitted by Anonymous on Sat, 06/26/2010 - 10:40.

How I can make a "Page Break"?


Thank you, Rajko

reply

‫ﺑﺮﺍﻣﺞ‬
Submitted by Anonymous on Mon, 05/17/2010 - 08:46.

Great additions, by the use of codes ‫ﺑﺮﺍﻣﺞ‬

reply

I am using following
Submitted by Anonymous on Wed, 02/24/2010 - 00:19.

I am using following code:


StringBuilder s = new StringBuilder();
//test aspx = new test();
StringWriter sw = new StringWriter(s);
HtmlTextWriter writer = new Html32TextWriter(sw);
base.Render(writer);

System.IO.Stream stream = new System.IO.FileStream(@"C:\new_pdf_name6.pdf", FileMode.OpenOrCreate);


Pdfizer.HtmlToPdfConverter htmlToPdf = new Pdfizer.HtmlToPdfConverter();
// open stream to write Pdf to to
htmlToPdf.Open(stream);
// write the HTML to the component
htmlToPdf.Run(s.ToString());
// close the write operation and complete the PDF file
htmlToPdf.Close();

This is giving following error on this statement:


htmlToPdf.Run(s.ToString());

Error is: The remote server returned an error: (503) Server Unavailable.

Please help
reply

can you add your source code


Submitted by Anonymous on Thu, 05/28/2009 - 04:22.

thank you for a quite nice html2pdf machine...


the thing is that i want to add new font styles with CP1250 encoding and it sounds impossible with this old
itextsharp.dll library, besides this one is not compatibile with the newest version.
the original code doesn't support [table],[br],[p] and so on...
Michal

reply

Image Support Updated


Submitted by Ben Hodson on Fri, 03/13/2009 - 13:01.

I think the reason your image support worked is because your image paths look to be local paths.  What most 
have tried and failed with is using images that are at URL paths on the web.  The component seems to have no 
functionality to download the images and put them in the PDF.  But it looks like if the images are a local path, it 
works.  I think that might reconcile the difference.

reply

Image Support WORKS!


Submitted by Anonymous on Thu, 03/12/2009 - 19:31.

I'm not sure why others had a problem with it, however, I was able to use this with images without any problem.
Below is the basic HTML that I generated (my images were 800X600).

<html>
<body>
<table height='800' width='600'>
<tr><td><img src='filepath1' /></td></tr>
<tr><td><img src='filepath2' /></td></tr>
<tr><td><img src='filepath3' /></td></tr>
</table>
</body>
</html>

This is a great little free method to get simple PDFs created. I highly recommend it!
reply

Demo Version - Winnovative Software Components


Damn and blast that really sucks.....
Submitted by Ryan (not verified) on Thu, 12/04/2008 - 05:20.

Damn and blast that really sucks..... Its unfortunate that there isn't a open source Html to Pdf converter that can
compete with the commercial solutions with out the ridiculous licensing costs. Thanks for the quick reply none
the less.

~3~

reply

hello
Submitted by Anonymous on Fri, 05/22/2009 - 13:42.

very cool realy thanks....evden eve nakliyat


evden eve nakliyat

reply

I hadn't tried images but it looks like


Submitted by Ben Hodson (not verified) on Wed, 12/03/2008 - 16:44.

I hadn't tried images but it looks like support is not good. It seems to just ignore your image URL's. Kind of
sucks. When I put this together, I only had need for text and formatting. Looks like that is as far as this
component goes.

reply

Great article, helped a ton, just one


Submitted by Ryan (not verified) on Wed, 12/03/2008 - 11:15.

Great article, helped a ton, just one question, what is the support for images like? I have a document rendered
in valid XHTML but by adding images the pdf only contains a single table cell, it doesn't contain any of the HTML I
had fed to it! Any ideas?

~3~

reply
has this been solved, have the same problem
Submitted by Anonymous on Mon, 04/12/2010 - 00:38.

Hi, i have the same problem i am dynamically building the html( i have checked and the output is correct
however it does not show anything on the pdf, any idea's

reply

Hi, i have the same problem i


Submitted by Anonymous on Sat, 05/22/2010 - 02:56.

Hi, i have the same problem i am dynamically building the html( i have checked and the output is
correct however it does not show anything on the pdf, any idea'sMedyum

reply

Simple and good article.


Submitted by Anonymous on Thu, 01/14/2010 - 16:31.

thank you for posting the code and necessary info.... its a great article.
There are so many paid products available. Some of the PDF converters are very close to the original
Windows XP cost. Thanks again !!

reply

Getting Error !!
Submitted by Anonymous on Thu, 01/14/2010 - 17:53.

Server Error in '/NewJcs' Application.


'0' is an unexpected token. The expected token is '"' or '''. Line 1, position 791.
Description: An unhandled exception occurred during the execution of the current web request. Please
review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Xml.XmlException: '0' is an unexpected token. The expected token is '"' or '''.
Line 1, position 791.

Source Error:

Line 492: htmlToPdf.Open(stream);


Line 493: // write the HTML to the component
Line 494: htmlToPdf.Run(sbHtml);
Line 495: // close the write operation and complete the PDF file
Line Demo Version - Winnovative Software Components
496: htmlToPdf.Close();

Source File: d:\Shared\Project\Under My Documents\NewJcs\App_Code\jwstore.cs Line: 494

Stack Trace:

[XmlException: '0' is an unexpected token. The expected token is '"' or '''. Line 1, position 791.]
System.Xml.XmlTextReaderImpl.Throw(Exception e) +76
System.Xml.XmlTextReaderImpl.Throw(String res, String[] args) +88
System.Xml.XmlTextReaderImpl.ThrowUnexpectedToken(String expectedToken1, String
expectedToken2) +104
System.Xml.XmlTextReaderImpl.ParseAttributes() +3978624
System.Xml.XmlTextReaderImpl.ParseElement() +343
System.Xml.XmlTextReaderImpl.ParseElementContent() +121
System.Xml.XmlTextReaderImpl.Read() +45
System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace) +58
System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) +20
System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
+129
System.Xml.XmlDocument.Load(XmlReader reader) +108
System.Xml.XmlDocument.LoadXml(String xml) +113
Pdfizer.HtmlToPdfConverter.Run(String html) +53
jwstore.html2pdf(String sbHtml, String sPathToWritePdfTo) in d:\Shared\Project\Under My
Documents\NewJcs\App_Code\jwstore.cs:494
Secure_confirmpayment.generate_text() in d:\Shared\Project\Under My
Documents\NewJcs\secured\confirmpayment1.aspx.cs:551
Secure_confirmpayment.Page_Load(Object sender, EventArgs e) in d:\Shared\Project\Under My
Documents\NewJcs\secured\confirmpayment1.aspx.cs:395
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean
includeStagesAfterAsyncPoint) +627

Version Information: Microsoft .NET Framework Version:2.0.50727.3603; ASP.NET


Version:2.0.50727.3082

reply
Demo Version - Winnovative Software Components

You might also like