Convert URL to PDF C#

Have you ever been in a situation where you wanted to save web pages as PDF documents? Converting HTML web pages from live URLs can be useful for a number of reasons. It allows saving the desired web pages for offline access like any other document. A PDF format makes it easy to highlight paragraphs, add comments, and edit the converted web page. As a result, the webpage is yours to customize as you see fit! In this article, we will learn how to convert URL to a PDF in C#.

The following topics will be covered in this article:

  1. Convert URL to PDF C# API
  2. Steps to Convert URL to PDF
  3. Convert Web Page from URL to PDF

Convert URL to PDF C# API

To convert a webpage from a live URL into a PDF document, we will be using the Aspose.HTML for .NET API. It is a feature rich API that enables us to create, modify, and extract data from HTML web pages or standalone HTML documents. It also allows conversion of HTML web pages and documents to supported formats without any external software.

The Url class of the API provides an object representation of a universal identifier (URL). We have the PdfSaveOptions class of the API that allows specifying various save options while saving as a PDF. We can convert supported documents to the popular formats using The Converter class. The ConvertHTML(Url, PdfSaveOptions, string) method of this class allows converting HTML to other formats.

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

PM> Install-Package Aspose.Html

How to Convert URL to PDF in C#

We can easily convert an HTML from URL to PDF by following the steps given below:

  • Specify the URL of a live web page.
  • Specify PDF Save Options.
  • Save URL content as a PDF.

The following section describes how to transform these steps into C# code and save HTML web page content from URL to PDF.

Convert URL to PDF using C#

We can save any webpage from a live URL to 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 a URL to a PDF in C#.

Convert URL to PDF using C#

Convert URL to PDF using C#

Get Free License

You can get a free temporary license to try Aspose.HTML for .NET without evaluation limitations.

Conclusion

In this article, we have learned how to convert an HTML content from a URL to a PDF in C#. Besides, you can learn more about Aspose.HTML for .NET API using documentation. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also