Have you ever wanted a function to convert HTML to PDF? It is really easy. A good use of this is when you have a resume on your website and you want to create a download of it as a PDF. Mind you; you will have to properly format your resume so it looks good in both cases. Not the easiest job, but totally worth while to have a resume download created on demand.

I just created a genPDF.aspx file that takes a URL and makes it to a PDF. Put a button on the page and have it call the genPDF.aspx page. Your download will automatically be created and pushed through to the browser.

Some things you will need first is a download of some zip files. You can get them from my website or you can seek them out on sourceforge ( iTextSharp / xmlworker ).

There is a lot of examples of how to do this; however, most examples do not support linked CSS files. My example does support linked CSS files.

The code is very simple:

     Document document = new Document();  
       var oStream = new MemoryStream();  
       PdfWriter writer = PdfWriter.GetInstance(document, oStream);  
       document.AddHeader("Content-Disposition", "attachment; filename=resume.pdf");  
       WebClient wc = new WebClient();  
       string htmlText = wc.DownloadString("");  
       iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, new StringReader(htmlText));  
       Response.AppendHeader("Content-Type", "application/pdf");  
       Response.AppendHeader("Content-Disposition", "attachment;filename=andrew_pallant_resume2015.pdf");  
       Response.OutputStream.Write(oStream.GetBuffer(), 0, oStream.GetBuffer().Length);  
     catch(Exception ex)  

Andrew Pallant (@LdnDeveloper) has been a web, database and desktop developer for over 16 years. Andrew has worked on projects that ranged from factory automation to writing business applications. Most recently he has been heavily involved in various forms for ecommerce projects. Over the years Andrew has worn many hats: Project Manager, IT Manager, Lead Developer, Supervisor of Developers and many more - See more at:

Posted in C#, C-Sharp, Developement, PDF, Web
One comment on “HTML to PDF
  1. Erik says:

    Hi Andrew,

    There is a pdf toolkit that I use every day that is definitely worth mentioning. To call this toolkit the Swiss Army Knif of pdf is an understatement. Besides the basics of stitching and splitting PDFs, it can render HTML pages and Word docs (without office) to PDF. Built-in compression and optimize work well too. The most important for me is its ability to placing text and images on pre made PDF forms. I also draw some forms and generate reports completely in code. This toolkit is AbcPDF from webSupergoo. I don’t work form them but have been a user since version 5.

    – Erik

Leave a Reply

Your email address will not be published. Required fields are marked *