Converting HTML content to PDF is a common requirement for archiving, sharing, or printing documents. The reliable .NET HTML to PDF library provided by Aspose.HTML enables you to convert HTML files, strings, and live URLs to high-quality PDF documents efficiently. In this article, you’ll learn how to utilize the .NET core HTML to PDF API to generate PDFs from HTML programmatically in C#, using practical examples and advanced customization options.
Topics Covered:
- C# API for HTML to PDF Conversion
- Generate PDF from HTML File
- Customize HTML to PDF Conversion
- Convert HTML String to PDF
- Convert Live URL to PDF
C# API to Generate PDF from HTML
Aspose.HTML for .NET is a feature-rich library offering seamless HTML to PDF conversion capabilities, along with support for other formats such as XPS and images. This library can be easily integrated into any .NET application and supports .NET core HTML to PDF conversion, allowing developers to programmatically convert HTML documents with high accuracy.
Key Features:
- Multiple Input Types: Effortlessly convert HTML files, strings, or live URLs to PDFs.
- Customizable Output: Modify save options, including resolution, compression, and watermarks for fine-tuned control.
- Cross-Platform Support: Fully compatible with Windows, macOS, and Linux.
Installation: You can add Aspose.HTML for .NET to your project using NuGet:
PM> Install-Package Aspose.Html
Generate PDF Document from HTML in C#
Generating a PDF document from an HTML file can be accomplished by following these simple steps:
- Load an HTML document using the HTMLDocument class.
- Create an instance of the PdfSaveOptions class.
- Call the Converter.ConvertHTML() method to generate the PDF. The method accepts HTMLDocument, PdfSaveOptions, and output PDF file path as arguments.
The following code sample illustrates how to generate a PDF document from an HTML file using C#.
Convert HTML to PDF with Save Options in C#
You can generate a PDF document from an HTML file while specifying additional save options by following these steps:
- Load the HTML document using the HTMLDocument class.
- Create an instance of the PdfSaveOptions class.
- Specify additional save options such as HorizontalResolution and VerticalResolution.
- Call the Converter.ConvertHTML() method for PDF generation.
The following code sample demonstrates how to generate a PDF from an HTML file with additional options in C#.
Generate PDF from HTML String in C#
You can also generate a PDF document from an HTML string by following these steps:
- Create an instance of the PdfSaveOptions class.
- Define any save options as needed.
- Call the Converter.ConvertHTML() method to generate the PDF. This method takes the HTML string, PdfSaveOptions, and output PDF file path as arguments.
The following code sample illustrates how to generate a PDF from an HTML string using C#.
Generate PDF from Live URL in C#
In the previous section, we covered generating a PDF from an HTML string. Now, let’s see how to save a webpage from a URL as a PDF document:
- Create an instance of the Url class with the URL of the webpage.
- Create an instance of the PdfSaveOptions class.
- Define any desired save options.
- Save the HTML as a PDF using the Converter.ConvertHTML() method, passing in the Url, PdfSaveOptions, and output PDF file path as arguments.
The following code sample depicts 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 great opportunity to explore the C# HTML to PDF API free trial and assess its features.
Conclusion
In this article, we’ve learned how to:
- Load an HTML file programmatically;
- Define PDF save options;
- Save an HTML string as PDF;
- Save a webpage as a PDF document;
- Convert HTML file to PDF in C#.
Whether you’re utilizing Aspose or exploring other .NET HTML to PDF open source alternatives, you can deepen your understanding of the Aspose.HTML for .NET API using documentation. If you have further questions, please feel free to reach out via our free support forum.
See Also
- [Create, Read, and Edit HTML Files in C#][18]
- [Download Image from URL in C#][19]
- [Convert Markdown to HTML in C#][20]