This article demonstrates some simple ways of HTML to PDF conversion using C#.
There are various scenarios when you need to perform HTML to PDF conversion. For example, you may want to convert a web page to PDF from within your application or you may need to generate PDF from the content of a WYSIWYG HTML editor. Another scenario could be converting the HTML page from a specific URL to PDF. In order to deal with such cases, you will learn how to perform HTML to PDF conversion. The HTML content could be a .html file or a web page from a URL.
- HTML to PDF Converter API
- HTML to PDF C# Conversion
- Convert HTML to password-protected PDF using C#
- Generate PDF from a Web Page in C#
- Get a Free API License
HTML to PDF C# Converter API – Free Download
NuGet Package Manager
Package Manager Console
PM> Install-Package Aspose.PDF
Steps to Convert HTML to PDF
The following are the steps to generate PDF from HTML using Aspose.PDF for .NET.
- Download and install the Aspose.PDF for .NET
- Add required namespaces of the API
- Load the HTML content from file or URL
- Generate PDF from provided HTML content
HTML to PDF C# Conversion
The following are the simple steps to generate PDF from HTML using Aspose.PDF for .NET.
- Create an instance of the HtmlLoadOptions class.
- Create an instance of Document class and initialize it with the HTML file’s path and HtmlLoadOptions object.
- Call Document.Save(String) method with the output PDF file’s name.
The following code sample shows how to convert HTML to PDF using C#.
Input HTML File
Converted PDF Document
C# HTML to PDF with Password-Protection
You can also convert HTML files to encrypted PDF documents using Aspose.PDF for .NET. The resultant PDF document can be protected with the user’s password, the owner’s password, access permissions, and cryptographic algorithm. You can encrypt the converted PDF using the Document.Encrypt() method. The following code sample shows how to convert an HTML file to an encrypted PDF.
C# Generate PDF from HTML using URL
Aspose.PDF for .NET also supports conversion from the live URLs. The following are the steps to generate PDF from a webpage using C#.
- Make a web request to the URL using WebRequest.
- Get the response of the web request into the Stream object.
- Create the Document object and initialize it with InputStream and HtmlLoadOptions objects.
- Save HTML stream as PDF using Document.Save() method.
The following code sample shows how to convert the web page to PDF in C#.
Get a Free API License
You can try the API without evaluation limitations by requesting a temporary license.
In this article, you have learned how to convert HTML content to PDF using programmatically using the .NET API. In addition to the simple conversion, this article also covered how to generate a password-protected PDF from HTML. You can easily port the provided code snippets to perform this conversion in VB.NET.
You may learn more about Aspose.PDF for .NET using the documentation.