Generate PDF from HTML in C#

Converting HTML content to PDF is essential for tasks such as archiving, sharing, or printing documents. The reliable .NET HTML to PDF library offered by Aspose.HTML for .NET simplifies the conversion process for HTML files, strings, and live URLs into high-quality PDF documents. This article will guide you through utilizing the .NET Core HTML to PDF API to programmatically generate PDFs from HTML in C#, complete with practical examples and advanced customization options.

Topics Covered

  1. C# API for HTML to PDF Conversion
  2. Generate PDF from HTML File
  3. Customize HTML to PDF Conversion
  4. Convert HTML String to PDF
  5. Convert Live URL to PDF

C# API to Generate PDF from HTML

Aspose.HTML for .NET is a powerful library that provides seamless HTML to PDF conversion capabilities, along with support for formats like XPS and images. This library can be easily integrated into any .NET application, enabling developers to convert HTML documents programmatically with high accuracy.

Key Features

  • Multiple Input Types: Effortlessly convert HTML files, strings, or live URLs to PDFs.
  • Customizable Output: Tailor save options, including resolution, compression, and watermarks for precise control.
  • Cross-Platform Support: Fully compatible with Windows, macOS, and Linux.

Installation: Add Aspose.HTML for .NET to your project via NuGet:

PM> Install-Package Aspose.Html

Generate PDF Document from HTML in C#

Creating a PDF document from an HTML file is a straightforward process. Follow these steps:

  1. Load an HTML document using the HTMLDocument class.
  2. Create an instance of the PdfSaveOptions class.
  3. Call the Converter.ConvertHTML() method, passing the HTMLDocument, PdfSaveOptions, and the output PDF file path as arguments.

Here’s a code sample demonstrating how to generate a PDF document from an HTML file using C#:

Generate PDF Document from HTML in C#

Convert HTML to PDF with Save Options in C#

To generate a PDF document from an HTML file while specifying additional save options, follow these steps:

  1. Load the HTML document using the HTMLDocument class.
  2. Create an instance of the PdfSaveOptions class.
  3. Specify any additional save options such as HorizontalResolution and VerticalResolution.
  4. Call the Converter.ConvertHTML() method for PDF generation.

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

For more customization options, refer to Advanced Save Options.

Generate PDF from HTML String in C#

Generating a PDF document from an HTML string can be accomplished with the following steps:

  1. Create an instance of the PdfSaveOptions class.
  2. Define any necessary save options.
  3. Call the Converter.ConvertHTML() method, which accepts the HTML string, PdfSaveOptions, and the output PDF file path as arguments.

Here’s a code sample demonstrating how to generate a PDF from an HTML string using C#:

Generate PDF from HTML String in C#

Generate PDF from Live URL in C#

Now, let’s explore how to save a webpage from a URL as a PDF document:

  1. Create an instance of the Url class with the webpage’s URL.
  2. Create an instance of the PdfSaveOptions class.
  3. Define any desired save options.
  4. Use the Converter.ConvertHTML() method to save the HTML as a PDF, passing in the Url, PdfSaveOptions, and the output PDF file path.

The following code sample shows how to generate a PDF from a live URL using C#:

Get Free License

You can get a free temporary license to try Aspose.HTML for .NET without evaluation limitations. This is a fantastic opportunity to explore the C# HTML to PDF API free trial and assess its features.

Conclusion

In this article, we learned how to:

  • Load an HTML file programmatically.
  • Define PDF save options.
  • Save an HTML string as a PDF.
  • Save a webpage as a PDF document.
  • Convert an HTML file to PDF in C#.

Whether you’re utilizing Aspose or considering other .NET HTML to PDF open source alternatives, you can deepen your understanding of the Aspose.HTML for .NET API by consulting the documentation. If you have any further questions, feel free to reach out via our free support forum.

See Also