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:
- Convert HTML to PDF in C# - API Installation
- Convert HTML to PDF
- C# HTML to PDF - Customize Conversion
- Convert HTML String to PDF
- 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:
- Firstly, load an HTML document using the HTMLDocument class.
- Next, create an instance of the PdfSaveOptions class.
- 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#.
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:
- Firstly, load an HTML document using the HTMLDocument class.
- Next, create an instance of the PdfSaveOptions class.
- After that, specify save options such as HorizontalResolution, VerticalResolution, BackgroundColor, etc.
- 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 String to PDF in C#
We can also convert an HTML string to a PDF document by following the steps given below:
- Firstly, define an HTML string
- Next, create an instance of the PdfSaveOptions class.
- Optionally, define save options.
- 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#.
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:
- Firstly, create an instance of the Url class with the URL of a webpage.
- Next, create an instance of the PdfSaveOptions class.
- Optionally, define save options.
- 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#.
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.