How to Convert HTML to PDF in ASP.NET

Written by kevin lee
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Convert HTML to PDF in ASP.NET
A third party component can solve your HTML to PDF conversion woes (young programmer image by Valentin Mosichev from

ASP.NET has thousands of native classes that perform a variety of functions (CE, see reference 2). PDF conversion is not one of those functions. Fortunately, third party software developers have created class libraries that you can use to perform these conversions. Simply add one of these libraries to your project and reference it. Your ASP.NET web form will then be able to generate PDF documents using any HTML file as a source.

Skill level:

Other People Are Reading

Things you need

  • Microsoft Visual Studio

Show MoreHide


  1. 1

    Download an "HTML to PDF" .NET class library. The Resources section contains links to several popular brands.

  2. 2

    Create a new ASP.NET website project and name it "MyProject". The page "Default.aspx" will appear in the solution explorer on the right of the screen.

  3. 3

    Switch to design mode and drag an ASP.NET button control onto the page.

  4. 4

    Double-click the button. This will add an event handler method named "Button1_Click" to the "Default.aspx.cs" code-behind page.

  5. 5

    Right click "MyProject" in the solution explorer and select "Add Reference". This will open the "Add Reference" window.

  6. 6

    Browse to the location of the library that you downloaded and select the DLL file located in that folder. All classes from the library will then be accessible from your ASP.NET project.

  7. 7

    Add a reference to the library in the "using" section of the "Default.aspx.cs" code file.

  8. 8

    Add "HTML to PDF" conversion code to the "Button1_Click" event handler. Different vendors will have different ways to set properties and access methods in their individual libraries. Usually the code block will look similar to this one:

    / create a new instance of the converter class / Converter converter = new Converter();

    / set properties for the PDF / converter.pageSIze = 500; converter.pagrOrientation = "Portrait"; etc;

    / get binary data from the target URL and store in the byte array /
    url = ""; byte[] bytes = converter.getDataFrom(URL_to_convert);

    / Setup response buffer / HttpResponse response = HttpContext.Current.Response; response.Clear(); response.AddHeader("Content-Type", "binary/octet-stream"); response.Flush(); response.BinaryWrite(downloadBytes); response.Flush(); response.End();

    The comments are self-explanatory. You will be able to set additional properties to control the final appearance of the PDF. All converter classes will require you to setup the HTTP Response object as shown. See the documentation that comes with your download for details on how to setup the call to the converter.

  9. 9

    Compile and run your project. Your browser will display your page containing the button.Click the button to execute the event handler code. It will retrieve the binary data from the specified URL and convert the raw HTML into a PDF document.

Tips and warnings

  • Normally you will put the conversion code shown above a separate class. You can then let each page create an instance of that class. This will improve the scalability of your ASP.NET website.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.