Generate PDF from HTML in C#

While browsing various websites, you may want to download the HTML content of a webpage as a PDF file for later use. In this case, you need an all-in-one converter to properly convert your content from HTML to PDF. As a C# developer, we can easily convert HTML content from HTML files to PDF documents. We can also generate a PDF document from an HTML string or save a webpage as a PDF from a live URL. In this article, we will learn how to generate a PDF from HTML in C#.

The following topics shall be covered in this article:

  1. C# API to Generate PDF from HTML
  2. Generate PDF Document from HTML
  3. Convert HTML to PDF with Save Options
  4. Generate PDF from HTML String
  5. Generate PDF from Live URL

C# API to Generate PDF from HTML

To generate a PDF document from an HTML file, we will be using the Aspose.HTML for .NET API. It is an advanced HTML processing API that allows creating, modifying, and extracting data from HTML documents. It also allows converting and rendering HTML documents without any external software.

The HTMLDocument class of the API represents an HTML document or the webpage that we see in the browser. The PdfSaveOptions class allows specifying various save options while saving as a PDF. We have the Converter class that provides a wide range of conversions to the popular formats, such as PDF, XPS, image formats, etc. It provides the ConvertHTML() methods to convert HTML to other formats.

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

PM> Install-Package Aspose.Html

Generate PDF Document from HTML in C#

We can easily generate a PDF document from an HTML file 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 to generate a PDF. It takes HTMLDocument, PdfSaveOptions, and output PDF file path as arguments.

The following code sample shows how to generate a PDF document from an HTML file using C#.

Generate-PDF-Document-from-HTML-in-CSharp

Generate PDF Document from HTML in C#

Convert HTML to PDF with Save Options in C#

We can generate a PDF document from an HTML file with additional save options 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, etc.
  4. Finally, call the Converter.ConvertHTML() method to generate a PDF.

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

Generate PDF from HTML String in C#

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

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

The following code sample shows how to generate a PDF from an HTML string using C#.

Generate-PDF-from-HTML-String-in-CSharp

Generate PDF from HTML String in C#

Generate PDF from Live URL in C#

In the previous section, we have seen how to generate a PDF from an HTML string. We can also save a webpage from a 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 generate a PDF from Live URL 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:

  • load an HTML file programmatically;
  • define PDF save options;
  • save HTML string as PDF;
  • save a webpage as a PDF document;
  • convert HTML file to PDF in C#.

Besides generating PDF from HTML in C#, 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