Create PDF from HTML

I’m working on a project at the moment that needs to generate a PDF document from data on the SQL Server.  The following steps illustrates the approach I’m using but in this article I want to focus on the HTML to PDF conversion using the C# port of iText, iTextSharp.

  • Select the required data into a .net DataSet
  • Create a new XmlDataDocument(DataSet) so that we have our data in XML
  • Create a new XSLT converter using XslCompiledTransform, load the XSLT file, then transform the XML to the output HTML we need
So the HTML I have is structured correctly and links to images that are required to be included in the PDF output.  In order to generate the PDF i went searching for a free PDF API and iTextSharp seems to be the commonly used from what I could tell.  I’ve had a quick read through their API documentation and put together a quick example of how to generate a PDF from an existing HTML file (so this doesn’t use the iTextSharp elements).  I hope this is useful (click on the image to enlarge):
To invoke the method, you simple pass in the source HTML file location (use MapPath() if on a web server) and where you want your output PDF to be created. Simples.
Advertisement

One Response to Create PDF from HTML

  1. Hello admin, I thought that your entry might be inspiration for me, and also for that valuable post, I\\\\\\\’d like to share it with my friends. I\\\\\\\’ll appreciate with you. Thanks my mate.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s