Convert HTML to PDF – C# Examples and Online Converter
In this guide, you will find information on how to convert an HTML document into a Portable Document Format (PDF) file format using Aspose.HTML for .NET library. We are going to cover in detail how to convert HTML to PDF using the ConvertHTML() methods of the Converter class, and how to apply PdfSaveOptions and ICreateStreamProvider parameters.
To continue following this tutorial, you should install and configure the Aspose.HTML for .NET library in your C# project. Our code examples help you to convert HTML to PDF and generate PDF files using the C# library.
HTML to PDF by a single line of code
HTML to PDF conversion is very popular. To perform this feature, Aspose.HTML for .NET offer the static methods of the Converter class as an understandable and straightforward way to convert HTML code into a PDF file literally with a single line of code!
1// Invoke the ConvertHTML() method to convert HTML to PDF
2Converter.ConvertHTML(@"<h1>Convert HTML to PDF!</h1>", ".", new PdfSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.pdf"));
Online HTML Converter
You can check the Aspose.HTML API functionality and convert HTML in real-time. Please load HTML from the local file system, select the output format and run the example. In the example, the save options are set by default. You will immediately receive the result as a separate file.
If you want to convert HTML to PDF programmatically, please see the following C# code examples.
Convert HTML to PDF in C#
Any HTML to PDF conversion you want to perform involves loading an HTML document and saving it in PDF format. You can load HTML from a file, HTML code, stream, or URL (see the Creating an HTML Document article). It can be different scenarios but it can be made with a few required steps:
- Load an HTML file using one of HTMLDocument() constructors of the
HTMLDocument class. In the example we use
HTMLDocument(
string
) constructor that initializes an HTML document from a file. - Create a new PdfSaveOptions object.
- Use the ConvertHTML() method of the Converter class to save HTML as a PDF file. You need to pass the HTMLDocument, PdfSaveOptions, and output file path to the ConvertHTML() method to convert HTML to PDF.
In order to continue in this guide, we will need some HTML file to work with. Here is a sample HTML file we will use in the next C# example – spring.html. If you open it in your browser, you should see:
Please take a look over the following C# code snippet that shows the HTML to PDF conversion process for the spring.html file.
1// Prepare a path to a source HTML file
2string documentPath = Path.Combine(DataDir, "spring.html");
3
4// Prepare a path to save the converted file
5string savePath = Path.Combine(OutputDir, "spring-output.pdf");
6
7// Initialize an HTML document from the file
8using var document = new HTMLDocument(documentPath);
9
10// Initialize PdfSaveOptions
11var options = new PdfSaveOptions();
12
13// Convert HTML to PDF
14Converter.ConvertHTML(document, options, savePath);
Save Options
The PDF creation functionality can be enhanced with save options per your needs. Aspose.HTML allows converting HTML to PDF using default or custom save options. PdfSaveOptions usage enables you to customize the rendering process; you can specify the page size, margins, file permissions, Css, etc.
Property | Description |
---|---|
JpegQuality | Specifies the quality of JPEG compression for images. The default value is 95. |
Css | Gets a CssOptions object which is used for configuration of CSS properties processing. |
DocumentInfo | This property contains information about the output PDF document. |
BackgroundColor | This property sets the color that will fill the background of every page. By default, this property is Transparent. |
PageSetup | This property gets a page setup object and uses it for configuration output page-set. |
HorizontalResolution | Sets horizontal resolution for output images in pixels per inch. The default value is 300 dpi. |
VerticalResolution | Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi. |
Encryption | This property gets or sets encryption details. If it is not set, then no encryption will be performed. |
To learn more about PdfSaveOptions, please read the Fine-Tuning Converters article. You can download the complete examples and data files from GitHub.
Convert HTML to PDF in C# using PdfSaveOptions
With Aspose.HTML, you can convert files programmatically with full control over a wide range of conversion parameters. To convert HTML to PDF with PdfSaveOptions specifying, you should follow a few steps:
- Load an HTML file using one of the HTMLDocument() constructors of the HTMLDocument class.
- Create a new PdfSaveOptions object and specify the required properties. The PdfSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting HTML to PDF format.
- Use the
ConvertHTML() method of the Converter class to save HTML as a PDF file. The ConvertHTML() method takes the
document
,options
, output file pathsavePath
and performs the conversion operation.
The following example shows how to use PdfSaveOptions and create a PDF file with custom save options:
1// Prepare a path to a source HTML file
2string documentPath = Path.Combine(DataDir, "drawing.html");
3
4// Prepare a path to save the converted file
5string savePath = Path.Combine(OutputDir, "drawing-options.pdf");
6
7// Initialize an HTML document from the file
8using var document = new HTMLDocument(documentPath);
9
10// Initialize PdfSaveOptions. Set up the page-size 600x300 pixels, margins, resolutions and change the background color to AliceBlue
11var options = new PdfSaveOptions()
12{
13 HorizontalResolution = 200,
14 VerticalResolution = 200,
15 BackgroundColor = System.Drawing.Color.AliceBlue,
16 JpegQuality = 100
17};
18options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(600, 300), new Margin(20, 10, 10, 10));
19
20// Convert HTML to PDF
21Converter.ConvertHTML(document, options, savePath);
In the above example, we use:
JpegQuality
property that enables you to specify the quality of JPEG compression for images;BackgroundColor
property that sets the color that will fill the background. The defaultBackgroundColor
is Transparent;HorizontalResolution
andVerticalResolution
properties that set horizontal/vertical resolution for output images in pixels per inch. By default, these properties are 300 dpi;PageSetup
property that specifies the page size and margins.
Converting HTML to PDF can be flexibly customized to get the desired result. In the following articles, you will find answers to popular questions concerning HTML to PDF conversion:
Output Stream Providers
If it is required to save files in the remote storage (e.g., cloud, database, etc.) you can implement ICreateStreamProvider interface to have manual control over the file creating process. This interface is designed as a callback object to create a stream at the beginning of the document/page (depending on the output format) and release the early created stream after rendering the document/page.
Aspose.HTML C# library allows realizing the MemoryStreamProvider
class as a custom implementation of the ICreateStreamProvider interface. The MemoryStreamProvider
class provides C# MemoryStream objects as output streams for writing data, which can be stored in memory as a stream:
1class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
2{
3 // List of MemoryStream objects created during the document rendering
4 public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
5
6 public Stream GetStream(string name, string extension)
7 {
8 // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats
9 MemoryStream result = new MemoryStream();
10 Streams.Add(result);
11 return result;
12 }
13
14 public Stream GetStream(string name, string extension, int page)
15 {
16 // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
17 MemoryStream result = new MemoryStream();
18 Streams.Add(result);
19 return result;
20 }
21
22 public void ReleaseStream(Stream stream)
23 {
24 // Here you can release the stream filled with data and, for instance, flush it to the hard-drive
25 }
26
27 public void Dispose()
28 {
29 // Releasing resources
30 foreach (var stream in Streams)
31 stream.Dispose();
32 }
33}
Aspose.HTML for .NET provides various types of output formats for rendering operations. Some of these formats produce a single output file (for instance, PDF, XPS), while others create multiple files (Image formats JPG, PNG, etc.).
The following C# code demonstrates how to use the MemoryStreamProvider
class and the Aspose.HTML for .NET library to convert HTML to PDF and save the result to a file.
1// Create an instance of MemoryStreamProvider
2using var streamProvider = new MemoryStreamProvider();
3
4// Initialize an HTML document
5using var document = new HTMLDocument(@"<h1>Convert HTML to PDF File Format!</h1>", ".");
6
7// Convert HTML to PDF using the MemoryStreamProvider
8Converter.ConvertHTML(document, new PdfSaveOptions(), streamProvider);
9
10// Get access to the memory stream that contains the result data
11var memory = streamProvider.Streams.First();
12memory.Seek(0, SeekOrigin.Begin);
13
14// Flush the result data to the output file
15using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.pdf")))
16{
17 memory.CopyTo(fs);
18}
You can download the complete examples and data files from GitHub.
Download our Aspose.HTML for .NET library allows you to successfully, quickly, and easily convert your HTML, MHTML, EPUB, SVG, and Markdown documents to the most popular formats.
Aspose.HTML offers a free online HTML to PDF Converter that converts HTML to PDF with high quality, easy and fast. Just upload, convert your files and get the result in a few seconds!