Convert HTML to PDF in C#

Have you ever wondered how to convert HTML to PDF in C#? If you are a developer, and you need to create a document that is readable on screen and can also be printed, converting HTML to PDF would be an ideal solution for your problem. You can use this article as a step-by-step guide on how exactly to convert HTML to PDF in C#. You will find detailed steps and code snippets that can help you solve this problem very easily.

HTML (Hyper Text Markup Language) is a markup language that allows the creation of web pages. We can convert and save web pages as PDF documents programmatically. There is no limitation to the amount of information we can put in a PDF file. Converting HTML to PDF allows saving websites or other standalone HTML web pages in a portable format or print them out. So, keep reading for detailed steps on how to convert HTML to PDF in C#.

The following topics shall be covered in this article:

  1. Convert HTML to PDF in C# - API Installation
  2. Convert HTML to PDF
  3. C# HTML to PDF - Customize Conversion
  4. Convert HTML String to PDF
  5. Convert HTML URL to PDF

Convert HTML to PDF in C# - API Installation

For converting HTML to PDF, we will be using the Aspose.HTML for .NET API. The API allows the creation of HTML documents. It also allows modifying, and extracting data from HTML documents. Furthermore, it enables you to convert and render HTML documents without any external software.

Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.Html

Convert HTML to PDF using C#

We can easily convert any HTML file to a PDF document by following the steps given below:

  1. Firstly, load an HTML document using the HTMLDocument class.
  2. Next, create an instance of the PdfSaveOptions class.
  3. Finally, call the Converter.ConvertHTML() method. It takes HTMLDocument, PdfSaveOptions, and output PDF file path as arguments.

The following code sample shows how to convert an HTML file to a PDF document in C#.

Convert HTML to PDF using C#

Convert HTML to PDF using C#

C# HTML to PDF - Customize Conversion

We can customize C# HTML to PDF conversion and set various additional save options specifically for saving HTML file as a PDF document by following the steps given below:

  1. Firstly, load an HTML document using the HTMLDocument class.
  2. Next, create an instance of the PdfSaveOptions class.
  3. After that, specify save options such as HorizontalResolution, VerticalResolution, BackgroundColor, etc.
  4. Finally, call the Converter.ConvertHTML() method to convert an HTML file into a PDF.

The following code sample shows how to convert an HTML file to a PDF with additional options in C#.

Convert HTML to PDF with Save Options in C#

Convert HTML to PDF with Save Options in C#

Convert HTML String to PDF in C#

We can also convert an HTML string to a PDF document by following the steps given below:

  1. Firstly, define an HTML string
  2. Next, create an instance of the PdfSaveOptions class.
  3. Optionally, define save options.
  4. Finally, call the Converter.ConvertHTML() method to convert an HTML string to a PDF. It takes HTML string, PdfSaveOptions and output PDF file path as arguments.

The following code sample shows how to convert an HTML string to a PDF document in C#.

Export HTML String to PDF in C#

Export HTML String to PDF in C#

Convert HTML URL to PDF in C#

We can also save any webpage from a live URL as a PDF document by following the steps given below:

  1. Firstly, create an instance of the Url class with the URL of a webpage.
  2. Next, create an instance of the PdfSaveOptions class.
  3. Optionally, define save options.
  4. Finally, save the HTML as a PDF using the Converter.ConvertHTML() method. It takes the Url, PdfSaveOptions, and output PDF file path as arguments.

The following code sample shows how to convert HTML to PDF from Live URL using C#.

Convert HTML to PDF from Live URL in C#

Convert HTML to PDF from Live URL in C#

C# HTML to PDF Converter - Free License

You can get a free temporary license to try Aspose.HTML for .NET without evaluation limitations and develop your own C# HTML to PDF converter.

HTML to PDF Converter C# - Read More

You can learn more to develop and enhance C# HTML to PDF Converter and explore other features of Aspose.HTML for .NET API using documentation. Please read more about classes and methods in the API references.

Convert HTML to PDF Online - C# free HTML to PDF

Please try the following free online HTML to PDF conversion tool, which is developed using the above API.

https://products.aspose.app/html/en/conversion/html-to-pdf

Conclusion

In this article, we have learned how to convert HTML to PDF in C#. We have also seen how to load an HTML file and specify PDF save options programmatically. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also