Convert HTML to PDF in C#

Have you ever wondered how to convert HTML to PDF in C#? If you need a document that looks good on screen and prints well, converting HTML to PDF is the perfect solution. This article provides a step‑by‑step guide with code snippets to help you convert HTML to PDF in C# quickly.

HTML (Hyper Text Markup Language) creates web pages. You can programmatically convert and save those pages as PDF documents without limits on content size. Converting HTML to PDF lets you store websites or standalone HTML pages in a portable format or print them. Keep reading for detailed steps on how to convert HTML to PDF in C#.

The following topics are covered:

  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

We use the Aspose.HTML for .NET API to convert HTML to PDF. The API creates, modifies, and extracts data from HTML documents and renders them to PDF without external software.

Download the DLL or install via NuGet:

PM> Install-Package Aspose.Html

Convert HTML to PDF using C#

Follow these steps to convert any HTML file to a PDF document:

  1. Load an HTML document with the HTMLDocument class.
  2. Create a PdfSaveOptions instance.
  3. Call Converter.ConvertHTML(), passing the HTMLDocument, PdfSaveOptions, and the output PDF path.

The code sample below 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

You can customize the conversion and set additional save options when converting an HTML file to PDF:

  1. Load an HTML document with the HTMLDocument class.
  2. Create a PdfSaveOptions instance.
  3. Set options such as HorizontalResolution, VerticalResolution, BackgroundColor, etc.
  4. Call Converter.ConvertHTML() to create the PDF.

The code sample below shows how to convert an HTML file to a PDF with extra 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#

To convert an HTML string to PDF:

  1. Define the HTML string.
  2. Create a PdfSaveOptions instance.
  3. (Optional) Set additional save options.
  4. Call Converter.ConvertHTML(), passing the HTML string, PdfSaveOptions, and the output PDF path.

The code sample below 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#

To save a live webpage as a PDF:

  1. Create a Url instance with the webpage address.
  2. Create a PdfSaveOptions instance.
  3. (Optional) Define save options.
  4. Call Converter.ConvertHTML(), passing the Url, PdfSaveOptions, and the output PDF path.

The code sample below shows how to convert HTML to PDF from a 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 limits and build your own C# HTML to PDF converter.

HTML to PDF Converter C# - Read More

Learn more about developing and enhancing a C# HTML to PDF converter and explore other features of Aspose.HTML for .NET via the documentation. See the API references for detailed class and method information.

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

Try the free online HTML to PDF conversion tool built with the same API:

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

Conclusion

In this article, we covered how to convert HTML to PDF in C#. You saw how to load an HTML file, apply PDF save options, and generate PDFs programmatically. If you have questions, visit our free support forum.

See Also