Programmatic HTML to PDF Conversion for .NET
Aspose.HTML for .NET converts HTML documents to PDF inside C# applications. Load HTML from a local file, URL, string, stream, or an in-memory HTMLDocument , configure the PDF result, and call Converter.ConvertHTML() .
Use this workflow to generate printable documents from HTML templates, archive web content as PDF, or add server-side HTML rendering to a .NET application. The conversion API processes the document structure, CSS, fonts, images, and other resources referenced by the source HTML.
C# Code Example and Interactive HTML to PDF Converter
Use the interactive example below to convert an HTML file or URL to PDF and review the corresponding C# code. The example loads an HTMLDocument, creates PdfSaveOptions, and passes both objects to Converter.ConvertHTML(). You can run the conversion with default settings, download the PDF result, and use the generated code as a starting point for your application.
using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Saving;
using HTMLDocument document = new HTMLDocument("{{input lower}}");
{{#if_output 'PDF'}}
PdfSaveOptions options = new PdfSaveOptions();
{{/if_output}}
{{#if_output 'MHTML'}}
MHTMLSaveOptions options = new MHTMLSaveOptions();
{{/if_output}}
{{#if_output 'DOCX'}}
DocSaveOptions options = new DocSaveOptions();
{{/if_output}}
{{#if_output 'XPS'}}
XpsSaveOptions options = new XpsSaveOptions();
{{/if_output}}
{{#if_output 'MD'}}
MarkdownSaveOptions options = new MarkdownSaveOptions();
{{/if_output}}
{{#if_output 'BMP' 'JPG' 'GIF' 'PNG' 'TIFF'}}
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.{{output param2 camel}});
{{/if_output}}
{{#if_output 'BMP' 'JPG' 'GIF' 'PNG' 'TIFF' 'PDF' 'MHTML' 'MD' 'XPS' 'DOCX'}}
Converter.ConvertHTML(document, options, "output.{{output lower}}");
{{/if_output}}
{{#if_output 'XHTML'}}
document.Save("output.xhtml", new HTMLSaveOptions() { DocumentType = HTMLSaveOptions.XHTML });
{{/if_output}}
How to Convert HTML to PDF in C#
- Load the source with an HTMLDocument constructor. The source can be a file, URL, stream, or HTML string.
- Create a PdfSaveOptions object. Keep the defaults or configure the required PDF output settings.
- Call Converter.ConvertHTML() with the document, save options, and output path.
- Open or return the generated PDF file. For custom output handling, use a
ConvertHTML()overload that accepts anICreateStreamProvider.
HTML to PDF Conversion Options and Documentation
Choose the API overload and output options that match your application:
- Input source: convert a local HTML file, a webpage URL, inline HTML content with a base URI, a stream, or an existing
HTMLDocument. - Resource resolution: provide a base URI when converting an HTML string so the API can resolve linked stylesheets, fonts, and images.
Read the complete
HTML to PDF guide
for examples covering different input sources and PDF settings. Use the ConvertHTML() overload reference and PdfSaveOptions reference when integrating the conversion into an application.
Use
PdfSaveOptions
to configure PDF page setup, margins, background color, image resolution, JPEG quality, document information, and encryption. Save to a file path or use
ICreateStreamProvider
when the application needs custom stream output.
HTML to PDF Conversion Notes
HTML to PDF is a rendering operation, so the result depends on the document context as well as the HTML markup. Check these points before moving the conversion into production:
- Relative resources need a base URI. When the source is an HTML string or stream, provide a base URI that lets the converter resolve relative stylesheets, fonts, and images. A file or URL source already supplies location context.
- Web layout becomes paged output. CSS controls the document presentation, while
PdfSaveOptions.PageSetupcontrols the PDF page size and margins. Test page breaks, long tables, and fixed-position elements with representative documents. - External resources must be available. Remote stylesheets, web fonts, and images must be accessible from the environment running the conversion. Use controlled resources when repeatable output is important.
- PDF settings should be explicit. Configure background color, JPEG quality, document information, or encryption in
PdfSaveOptionswhen the output has corresponding delivery or security requirements. - Choose the output destination deliberately. Save directly to a file for a simple workflow, or use ICreateStreamProvider when the application needs custom stream handling.
Install Aspose.HTML for .NET
Add the package to your project, then use the C# example above as the minimal HTML to PDF workflow.
Install Aspose.HTML for .NET from NuGet with the .NET CLI:
dotnet add package Aspose.HTML
You can also use the NuGet Package Manager UI or run Install-Package Aspose.HTML in the Package Manager Console. See the
installation guide
for setup details, or request a
temporary license
to evaluate the API without evaluation limitations.
HTML to PDF Conversion FAQ
1. How do I convert HTML to PDF in C#?
Load the source into an HTMLDocument, create PdfSaveOptions, and pass them with the output path to Converter.ConvertHTML(). The interactive C# example above shows this minimal workflow.
2. Can I convert an HTML string or webpage URL to PDF?
Yes. Converter.ConvertHTML() supports HTML supplied as a file, string, stream, URL, or HTMLDocument. When converting inline HTML, provide a base URI if the document uses relative CSS, font, or image paths.
3. How do I set the PDF page size and margins?
Configure the PageSetup property of PdfSaveOptions before conversion. It controls the PDF page dimensions and margins independently of the HTML content styling.
4. Are CSS, web fonts, and images preserved in the PDF?
The conversion engine renders CSS, fonts, and images available to the source document. Relative resources need a valid base URI, and remote resources must be accessible from the environment running the conversion.
5. Can I save the generated PDF to a stream?
Yes. Use a Converter.ConvertHTML() overload with an ICreateStreamProvider implementation when the application needs custom stream-based output instead of a file path.
Other Supported HTML Conversions
Explore other HTML conversion APIs and C# examples: